Permalink
Browse files

Initial reorg

  • Loading branch information...
1 parent a5201de commit d02a00fa1e95651ed35bd8d8d597120fcd70696d @mattkenn4545 mattkenn4545 committed Apr 27, 2012
File renamed without changes.
Oops, something went wrong.
@@ -1,14 +1,15 @@
import time
import os
-from nicetime import getnicetimefromdatetime, getdatetimefromnicetime
+from nicetime import getdatetimefromnicetime
from datetime import datetime
+
class NagCommands(object):
def __init__(self, nag):
self.nag = nag
-
- def scheduledowntime(self, author, starttime, endtime, comment, apikey = 'default', doappend = False):
+
+ def scheduledowntime(self, author, starttime, endtime, comment, apikey='default', doappend=False):
TIMEFORMAT = '%Y%m%d%H%M'
try:
start = int(time.mktime(time.strptime(starttime, TIMEFORMAT)))
@@ -24,10 +25,10 @@ def scheduledowntime(self, author, starttime, endtime, comment, apikey = 'defaul
end = int(time.mktime(getdatetimefromnicetime(endtime, datetime.fromtimestamp(start)).timetuple()))
except Exception:
return 'Error: "EndTime" not in correct format.'
-
- values = {'fixed': 1, 'trigger_id': 0, 'duration': 0, 'author': author,
+
+ values = {'fixed': 1, 'trigger_id': 0, 'duration': 0, 'author': author,
'start_time': start, 'end_time': end, 'comment': comment}
-
+
if self.nag.classname() == 'servicegroup':
values['servicegroup_name'] = self.nag.servicegroup_name
command = 'SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;<servicegroup_name>;<start_time>;<end_time>;<fixed>;<trigger_id>;<duration>;<author>;<comment>'
@@ -43,10 +44,10 @@ def scheduledowntime(self, author, starttime, endtime, comment, apikey = 'defaul
for value in values:
command = command.replace('<' + value + '>', str(values[value]))
-
+
if command.find('<') > 0:
return 'Error: Incomplete Nagios command file format substitution '
-
+
command = '[' + str(int(time.time())) + '] ' + command
if doappend:
@@ -60,5 +61,5 @@ def scheduledowntime(self, author, starttime, endtime, comment, apikey = 'defaul
except Exception, e:
print e
return 'Error: Appending to the Nagios command file'
-
- return command
+
+ return command
@@ -1,48 +1,50 @@
import os
import types
+
class NagConfig(object):
- STALE_THRESHOLD = 240 #Should be set to Nagios check timeout or the longest time in seconds a check might take
+ STALE_THRESHOLD = 240
IGNORE_STALE_DATA = False
NAGIOS_CMD_FILE = '/var/lib/nagios3/rw/nagios.cmd'
IMPORTANTSERVICEGROUPS = {}
basicAPIKEYS = []
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
REQUIRE_HARD_SERVICE_STATUS = False
-
+
def __init__(self, files):
allfilesexist = True
- for file in files:
- if not os.path.exists(file):
+ for temp in files:
+ if not os.path.exists(temp):
allfilesexist = False
- print '{0} does not exist'.format(file)
-
+ print '{0} does not exist'.format(temp)
+
if allfilesexist:
self.files = files
self.getpermissions = basicgetpermissions
else:
raise IOError('File(s) not found')
-
+
def _set_getpermissionsfunction(self, function):
self.getpermissions = function
-
+
def _get_basicapikeys(self):
return NagConfig.basicAPIKEYS
-
+
def _set_basicapikeys(self, apikeys):
if type(apikeys) is not types.ListType:
apikeys = [apikeys]
NagConfig.basicAPIKEYS = apikeys
-
+
APIKEYS = property(_get_basicapikeys, _set_basicapikeys)
-
+
+
def basicgetpermissions(apikey):
'''Basic apikey check function. Can be overridden to provide custody apikey validation functionality.
- Should return a list of permissions or an empty list of no permissions.
+ Should return a list of permissions or an empty list of no permissions.
Permissions are currently un-utilized but future versions of the NagCommands() will use them to
restrict access etc...'''
if apikey in NagConfig.basicAPIKEYS:
return ['access granted']
else:
- return []
+ return []
@@ -1,21 +1,20 @@
class NagList(list):
def __getattr__(self, name):
-
if name == 'first':
if self:
return self[0]
else:
return None
-
+
if name == 'names':
return [x.name for x in self]
-
+
obj = [x for x in self if x.name == name]
if obj:
if len(obj) == 1:
return obj[0]
else:
raise AttributeError('Multiple instances found')
- raise AttributeError
+ raise AttributeError
No changes.
No changes.
No changes.
@@ -4,19 +4,20 @@
from Nag import Nag
from NagList import NagList
+
def parse(config):
tempobjs = []
-
+
files = config.files
importantservicegroups = config.IMPORTANTSERVICEGROUPS
-
+
nag = None
for filename in files:
tempfile = open(filename)
content = tempfile.read()
tempfile.close()
-
+
if nag == None:
nag = Nag()
if '.cache' in filename:
@@ -25,20 +26,20 @@ def parse(config):
sectionsnames = ['hoststatus', 'servicestatus', 'programstatus']
else:
raise Exception('Invalid filename detected')
-
+
for section in sectionsnames:
- pat = re.compile(section +' \{([\S\s]*?)\}', re.DOTALL)
-
+ pat = re.compile(section + ' \{([\S\s]*?)\}', re.DOTALL)
+
for sectioncontent in pat.findall(content):
- if section == 'hoststatus':
+ if section == 'hoststatus':
temp = Nag.Host(nag)
- elif section == 'servicestatus':
+ elif section == 'servicestatus':
temp = Nag.Service(nag)
elif section == 'programstatus':
temp = nag
elif section == 'define servicegroup':
temp = Nag.ServiceGroup(nag)
-
+
for attr in sectioncontent.splitlines():
attr = attr.strip()
if len(attr) == 0 or attr.startswith('#'):
@@ -50,7 +51,7 @@ def parse(config):
delim = '='
shortattr = attr.split(delim)[0].lower()
- temp.__dict__[shortattr] = attr.replace(shortattr+delim, '')
+ temp.__dict__[shortattr] = attr.replace(shortattr + delim, '')
tempobjs.append(temp)
hosts = [x for x in tempobjs if isinstance(x, Nag.Host)]
@@ -59,7 +60,7 @@ def parse(config):
nag.importantservicegroups = importantservicegroups
nag.config = config
-
+
if len(hosts):
nag.hosts = NagList(hosts)
if len(services):
@@ -71,4 +72,4 @@ def parse(config):
if __name__ == "__main__":
- pass
+ pass
@@ -1,7 +1,8 @@
from datetime import datetime, timedelta
from string import find
-def getnicetimefromdatetime(datetimeinput, basedatetime = None):
+
+def getnicetimefromdatetime(datetimeinput, basedatetime=None):
if basedatetime is None:
base = datetime.now()
else:
@@ -25,7 +26,8 @@ def getnicetimefromdatetime(datetimeinput, basedatetime = None):
return result
-def getdatetimefromnicetime(nicetime, basedatetime = None):
+
+def getdatetimefromnicetime(nicetime, basedatetime=None):
if basedatetime is None:
base = datetime.now()
else:
@@ -50,8 +52,5 @@ def getdatetimefromnicetime(nicetime, basedatetime = None):
return base + delta
-
-
-
if __name__ == "__main__":
pass
@@ -0,0 +1,6 @@
+from Services.nagfactory import parse
+from Services.nicetime import getnicetimefromdatetime, getdatetimefromnicetime
+
+from Model.Nag import Nag
+from Model.NagConfig import NagConfig
+
View
@@ -23,7 +23,9 @@ def examplegetpermissionssoverridefunction(apikey):
#Do something interesting
-print nag.services.first
+servicegroupstatuses = [x.status for x in nag.getservicegroups(onlyimportant = False)]
+for status in list(set(servicegroupstatuses)):
+ print servicegroupstatuses.count(status)
#json = nag.genoutput('json', prittyprint = False)
@@ -1,4 +0,0 @@
-from factory import parse
-from Nag import Nag
-from NagConfig import NagConfig
-from nicetime import getnicetimefromdatetime, getdatetimefromnicetime
Oops, something went wrong.

0 comments on commit d02a00f

Please sign in to comment.