|
@@ -12,15 +12,23 @@ django.setup()
|
|
|
import optparse
|
|
|
import sys
|
|
|
|
|
|
+from django.db.models import Max
|
|
|
from django.http import QueryDict
|
|
|
|
|
|
+from plugin.models import PluginMemory
|
|
|
+
|
|
|
from traps.models import Trap
|
|
|
from traps.tools import get_filtered_traps_for_querydict
|
|
|
|
|
|
+from states.models import State
|
|
|
+from states.tools import get_filtered_states_for_querydict
|
|
|
+
|
|
|
##############################################################################
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
|
|
+parser.add_option('', '--states', dest='check_states', action='store_true', default=False, help='Check for States instead of Traps')
|
|
|
+
|
|
|
parser.add_option('-H', '--hostname', dest='hostname', default=None, help='Filter hostname(s), separated by ","')
|
|
|
parser.add_option('-S', '--severity', dest='severity', default=None, help='Filter severity(s), separated by ","')
|
|
|
parser.add_option('-C', '--category', dest='category', default=None, help='Filter category(s), separated by ","')
|
|
@@ -58,10 +66,16 @@ if opts.exclude_severity:
|
|
|
if opts.exclude_category:
|
|
|
[ query.update( {u'exclude_category': category} ) for category in opts.exclude_category.split(',') ]
|
|
|
|
|
|
-if opts.verb >=2:
|
|
|
- print(query)
|
|
|
+if opts.verb >=3:
|
|
|
+ from pprint import pprint
|
|
|
+ pprint(query)
|
|
|
|
|
|
-traps = get_filtered_traps_for_querydict(query)
|
|
|
+if opts.check_states:
|
|
|
+ objs = get_filtered_states_for_querydict(query)
|
|
|
+ objname = 'state'
|
|
|
+else:
|
|
|
+ objs = get_filtered_traps_for_querydict(query)
|
|
|
+ objname = 'trap'
|
|
|
|
|
|
if opts.memory:
|
|
|
try:
|
|
@@ -85,12 +99,13 @@ if opts.verb >=2:
|
|
|
print 'Maxid is %s' % maxid
|
|
|
|
|
|
if maxid:
|
|
|
- traps = traps.filter(id__gt=maxid)
|
|
|
+ objs = objs.filter(id__gt=maxid)
|
|
|
|
|
|
if opts.verb >=3:
|
|
|
- for trap in traps:
|
|
|
- print trap
|
|
|
- maxid = max(maxid, trap.id)
|
|
|
+ for obj in objs:
|
|
|
+ print obj
|
|
|
+
|
|
|
+maxid = max( objs.aggregate(Max('id')).values()[0], maxid )
|
|
|
|
|
|
if opts.verb >=2:
|
|
|
print 'Max ID: %s' % maxid
|
|
@@ -99,22 +114,22 @@ if opts.memory:
|
|
|
mem.maxid = maxid
|
|
|
mem.save()
|
|
|
|
|
|
-warn_cnt = traps.filter(severity='WARNING').count()
|
|
|
-crit_cnt = traps.filter(severity='CRITICAL').count()
|
|
|
+warn_cnt = objs.filter(severity='WARNING').count()
|
|
|
+crit_cnt = objs.filter(severity='CRITICAL').count()
|
|
|
if opts.verb >=2:
|
|
|
- print 'Warning traps: %s' % warn_cnt
|
|
|
- print 'Critical traps: %s' % crit_cnt
|
|
|
+ print 'Warning %ss: %s' % (objname, warn_cnt)
|
|
|
+ print 'Critical %ss: %s' % (objname, crit_cnt)
|
|
|
|
|
|
out = []
|
|
|
retcode = 0
|
|
|
|
|
|
if crit_cnt:
|
|
|
retcode = max(2, retcode)
|
|
|
- out.append('%s critical traps' % crit_cnt)
|
|
|
+ out.append('%s critical %ss' % (crit_cnt, objname) )
|
|
|
if warn_cnt:
|
|
|
retcode = max(1, retcode)
|
|
|
- out.append('%s warning traps' % warn_cnt)
|
|
|
+ out.append('%s warning %ss' % (warn_cnt, objname) )
|
|
|
|
|
|
-print 'Traps %s: %s%s' % (retstring[retcode], ' and '.join(out), out and ' found in DB' )
|
|
|
+print 'MyMonTools/%ss %s: %s%s' % (objname, retstring[retcode], ' and '.join(out), out and ' found in DB' or 'no warning or critical %s found' % objname)
|
|
|
sys.exit(retcode)
|
|
|
|