moved linspector to bin and renamed lib

This commit is contained in:
Johannes Findeisen 2013-10-08 22:16:31 +02:00
commit 4f6639b259
47 changed files with 24 additions and 161 deletions

View file

View file

@ -0,0 +1,70 @@
"""
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import subprocess as sp
from subprocess import Popen
from subprocess import CalledProcessError
from datetime import datetime as dt
#TODO: Move this to shell.py service file. this definitely is shell execution. (hanez)
class Command:
def __init__(self, command, log):
self.command = command
self.log = log
self.output = None
self.error = None
self.retcode = 0
self.commandStart = 0
def __str__(self):
return self.command
def call(self):
# self.commandStart = dt.now()
# "called at: "
# process = sp.Popen(stdout=PIPE, *popenargs, **kwargs)
# self.output, self.error = process.communicate()
# self.retcode = process.poll()
try:
self.commandStart = dt.now()
self.log.info("calling command " + str(self.command) + " at " + str(self.commandStart))
#self.output=sp.check_output(self.command.split())
process = Popen(self.command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
self.output, self.error = process.communicate()
self.log.debug(str(self.output))
self.log.debug(str(self.error))
self.retcode = process.poll()
except CalledProcessError:
self.error = CalledProcessError.output
self.retcode = CalledProcessError.returncode
def getOutput(self):
return self.output
def getError(self):
return self.error
def getAllOutput(self):
return str(self.output) + str(self.error) + str(self.retcode)
def getReturnCode(self):
return self.retcode

153
linspector/core/daemon.py Normal file
View file

@ -0,0 +1,153 @@
"""
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import os
import time
import atexit
from signal import SIGTERM
class Daemon:
"""
A generic daemon class.
Usage: subclass the Daemon class and override the run() method
"""
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.pidfile = pidfile
def daemonize(self):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
file(self.pidfile, 'w+').write("%s\n" % pid)
def delpid(self):
os.remove(self.pidfile)
def start(self):
"""
Start the daemon
"""
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if pid:
message = "pidfile %s already exist. daemon already running?\n"
sys.stderr.write(message % self.pidfile)
sys.exit(1)
# Start the daemon
self.daemonize()
self.run()
def stop(self):
"""
Stop the daemon
"""
# Get the pid from the pidfile
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
message = "pidfile %s does not exist. daemon not running?\n"
sys.stderr.write(message % self.pidfile)
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
except OSError, err:
err = str(err)
if err.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print str(err)
sys.exit(1)
def restart(self):
"""
Restart the daemon
"""
self.stop()
self.start()
def run(self):
"""
You should override this method when you subclass Daemon.
It will be called after the process has been
daemonized by start() or restart().
"""

View file

@ -0,0 +1,25 @@
"""
The interface class should contain all stuff for frontend/backend communication to the Linspector core.
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
class Interface():
def __init__(self):
pass

122
linspector/core/job.py Normal file
View file

@ -0,0 +1,122 @@
"""
This is what job_function needs as parameter for each job to successfully
execute.
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from datetime import datetime
def generateId():
i = 0
while True:
yield i
i += 1
class Job:
def __init__(self, service, host, members, processors, core):
self.service = service
self.host = host
self.members = members
self.processors = processors
self.core = core
self.jobInfos = []
self.jobThreshold = 0
def __str__(self):
return str(self.__dict__)
def set_logger(self, log):
self.log = log
def set_job(self, job):
self.job = job
def handle_threshold(self, jobInfo, serviceThreshold, executionSucessful):
if executionSucessful:
if self.jobThreshold > 0:
self.jobThreshold -= 1
else:
self.jobThreshold += 1
if self.jobThreshold >= serviceThreshold:
self.log.debug("Threshold reached!")
self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold)
def handle_alarm(self, jobInfo, thresholdOffset):
for member in self.service.get_hostgroup().get_members():
#TODO: Put Tasks in a run queue and execute them in a background thread. FIFO! Reduces delay in core.
for task in member.get_tasks():
task.execute(jobInfo.get_message(), self.core)
def handle_call(self):
self.log.debug("handle call")
self.log.debug(self.service)
try:
jobInfo = JobInfo(self.host, self.service)
self.service._execute(jobInfo)
jobInfo.set_execution_end()
self.handle_threshold(jobInfo, self.service.get_threshold(), jobInfo.was_execution_successful())
self.log.debug("Code: " + str(jobInfo.get_errorcode()) + ", Message: " + str(jobInfo.get_message()))
self.jobInfos.append(jobInfo)
except Exception, e:
self.log.debug(e)
class JobInfo(object):
def __init__(self, host, service):
self.id = generateId()
self.host = host
self.service = service
self.executionBegin = datetime.now()
self._errorcode = -1
self._message = None
self._executionSuccess = False
def get_host(self):
return self.host
def set_result(self, result):
self.result = result
def set_execution_end(self):
self.executionEnd = datetime.now()
def set_execution_successful(self, successful):
self._executionSuccess = successful
def was_execution_successful(self):
return self._executionSuccess
def set_message(self, msg):
self._message = msg
def get_message(self):
return self._message
def set_errorcode(self, errcode):
self._errorcode = errcode
def get_errorcode(self):
return self._errorcode

View file

@ -0,0 +1,42 @@
"""
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from ..core import logger
from ..core.daemon import Daemon
"""
TODO: Think about, that daemonizing this software is not our goal but when we want to that, this needs a rewrite and
should maybe move over to the daemon frontend. daemon.py will remain the the base for this and should stay in linspector/core .
Since a daemon it normally not a frontend we should think about how to handle this. When daemonizing a real frontend
like "Lish" will make no sense but a frontend like "https" or "xmpp" could be useful anyway...
"""
class LinspectorDaemon(Daemon):
def run(self):
while True:
try:
a = 2
logger.writeLogToFile(_logfile, "Running!")
print "running!"
except Exception as err:
#logger.writeLogToFile(_logfile, str(err))
print "failed"
sys.exit(1)
time.sleep(1)

77
linspector/core/logger.py Normal file
View file

@ -0,0 +1,77 @@
"""
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
import logging.handlers
import os
import os.path as path
class Logger():
"""
Logger class that prints its messages and keeps them also inside a logfile
"""
def __init__(self, logfile="./linspector.log", logLevel=logging.DEBUG, logfileLevel=logging.DEBUG):
"""
initializes a new Logger object.
:param logLevel: the LoggingLevel from the console output (DEBUG default)
:param logfile: the file where to log. Logs are rotated by default.
:param logfileLevel: the LoggingLevel for the file Logger. (DEBUG default)
"""
logfile = path.expanduser(logfile)
if not path.exists(path.dirname(logfile)):
os.makedirs(path.dirname(logfile))
self.log = logging.getLogger("LinspectorLogger")
self.log.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logLevel)
fileHandler = logging.handlers.RotatingFileHandler(logfile, maxBytes=1024000, backupCount=4)
fileHandler.setLevel(logfileLevel)
consoleFormatter = logging.Formatter('[%(levelname)s]: %(message)s')
fileFormatter = logging.Formatter('%(asctime)s [%(levelname)s]: %(message)s')
consoleHandler.setFormatter(consoleFormatter)
fileHandler.setFormatter(fileFormatter)
self.log.addHandler(consoleHandler)
self.log.addHandler(fileHandler)
def d(self, message):
self.log.debug(message)
def i(self, message):
self.log.info(message)
def w(self, message):
self.log.warn(message)
def e(self, message):
self.log.error(message)
def c(self, message):
self.log.critical(message)
def close(self):
logging.shutdown()

View file

@ -0,0 +1,25 @@
"""
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from apscheduler.scheduler import Scheduler
class Scheduler(Scheduler):
def test(self):
pass