Guest User

Untitled

a guest
Oct 17th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import MySQLdb
  4. import socket
  5. import itertools
  6.  
  7.  
  8. REKLAMA_DB = dict(
  9.     host="10.6.100.218",
  10.     user="monitoring",
  11.     passwd="XXX",
  12.     db="reklama",
  13. )
  14.  
  15. MONITORING_DB = dict(
  16.     host= "10.3.100.23",
  17.     user= "monitor",
  18.     passwd= "XXX",
  19.     db= "monitoring",
  20. )
  21.  
  22. SERVICE = "rbmap"
  23. MONHOST = socket.gethostname()
  24.  
  25. excludehosts = set([ 'scoop.corp.mail.ru', ])
  26.  
  27. conn = MySQLdb.connect (**REKLAMA_DB)
  28. cursor = conn.cursor()
  29. cursor.execute("select inet_ntoa(ip),hostname,from_unixtime(mtime)  from host where mtime<(UNIX_TIMESTAMP(now()) - 3600 ) and hostname <> ''")
  30. L = cursor.fetchall()
  31. cursor.close()
  32. conn.close()
  33.  
  34. conn = MySQLdb.connect (**MONITORING_DB)
  35. cursor = conn.cursor ()
  36.  
  37. def setstate(server,status,msg):
  38.     cursor.execute("SELECT id FROM states WHERE server=%s AND service=%s AND subservice=%s AND monitorhost=%s", ( server, SERVICE, "", MONHOST ) )
  39.  
  40.     try:
  41.         id = cursor.fetchone()[0]
  42.     except TypeError:
  43.         id = None
  44.     if status > 0:
  45.         if id:
  46.             cursor.execute("UPDATE states SET status=%s,msg=%s,checktime=NOW() WHERE id=%s", ( status, msg, id ) )
  47.         else:
  48.             cursor.execute("REPLACE INTO states (server,service,subservice,status,msg,checktime,starttime,monitorhost) VALUES (%s,%s,%s,%s,%s,NOW(),NOW(),%s)
  49. ", ( server, SERVICE, "", status, msg, MONHOST) )
  50.     else:
  51.         if id:
  52.             cursor.execute("DELETE FROM states WHERE id=%s", ( id, ) )
  53.  
  54.  
  55. for (ip,server,mtime) in L:
  56.     if server not in excludehosts:
  57.         msg = "rbmap not updated: %s\t[Last update: %s]" % ( ip,mtime );
  58.         print msg;
  59.         setstate(server,1,msg)
  60.     else:
  61.         print "Host %s skipped" % ( server, )
  62.  
  63. cursor.execute("SELECT server FROM states WHERE service=%s and status>0", (SERVICE,) )
  64. badhosts = set(itertools.chain(*cursor.fetchall()))
  65. servers = set(map(lambda a: a[1], L))
  66.  
  67. for server in (badhosts - ( servers - excludehosts ) ):
  68.     setstate(server,0,"")
  69.  
  70. cursor.close()
  71. conn.close()
Add Comment
Please, Sign In to add comment