Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import MySQLdb
- import socket
- import itertools
- REKLAMA_DB = dict(
- host="10.6.100.218",
- user="monitoring",
- passwd="XXX",
- db="reklama",
- )
- MONITORING_DB = dict(
- host= "10.3.100.23",
- user= "monitor",
- passwd= "XXX",
- db= "monitoring",
- )
- SERVICE = "rbmap"
- MONHOST = socket.gethostname()
- excludehosts = set([ 'scoop.corp.mail.ru', ])
- conn = MySQLdb.connect (**REKLAMA_DB)
- cursor = conn.cursor()
- cursor.execute("select inet_ntoa(ip),hostname,from_unixtime(mtime) from host where mtime<(UNIX_TIMESTAMP(now()) - 3600 ) and hostname <> ''")
- L = cursor.fetchall()
- cursor.close()
- conn.close()
- conn = MySQLdb.connect (**MONITORING_DB)
- cursor = conn.cursor ()
- def setstate(server,status,msg):
- cursor.execute("SELECT id FROM states WHERE server=%s AND service=%s AND subservice=%s AND monitorhost=%s", ( server, SERVICE, "", MONHOST ) )
- try:
- id = cursor.fetchone()[0]
- except TypeError:
- id = None
- if status > 0:
- if id:
- cursor.execute("UPDATE states SET status=%s,msg=%s,checktime=NOW() WHERE id=%s", ( status, msg, id ) )
- else:
- cursor.execute("REPLACE INTO states (server,service,subservice,status,msg,checktime,starttime,monitorhost) VALUES (%s,%s,%s,%s,%s,NOW(),NOW(),%s)
- ", ( server, SERVICE, "", status, msg, MONHOST) )
- else:
- if id:
- cursor.execute("DELETE FROM states WHERE id=%s", ( id, ) )
- for (ip,server,mtime) in L:
- if server not in excludehosts:
- msg = "rbmap not updated: %s\t[Last update: %s]" % ( ip,mtime );
- print msg;
- setstate(server,1,msg)
- else:
- print "Host %s skipped" % ( server, )
- cursor.execute("SELECT server FROM states WHERE service=%s and status>0", (SERVICE,) )
- badhosts = set(itertools.chain(*cursor.fetchall()))
- servers = set(map(lambda a: a[1], L))
- for server in (badhosts - ( servers - excludehosts ) ):
- setstate(server,0,"")
- cursor.close()
- conn.close()
Add Comment
Please, Sign In to add comment