Guest User

Untitled

a guest
Aug 19th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. import sys
  3. import MySQLdb
  4. import ConfigParser
  5. config=ConfigParser.ConfigParser()
  6. config.readfp(open('/tmp/DNS.conf'))
  7. pick_list=config.get('DNS','ip')
  8. hostlist=map(str,pick_list.split(','))
  9.  
  10. monitor_user=config.get('check_mysql','username')
  11. monitor_password=config.get('check_mysql','passwd')
  12.  
  13. def pingcmd(ip,port):
  14. import socket
  15. sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16. sk.settimeout(2)
  17. try:
  18. sk.connect((ip,port))
  19. except Exception:
  20. print "exception %s %s"%(ip,port)
  21. sk.close()
  22. return (ip,port,'fail')
  23. return (ip,port,'sucss')
  24.  
  25.  
  26. def runCmd(cmd,ip,port):
  27. cnx = MySQLdb.connect(user=monitor_user,host=ip,passwd=monitor_password,port=port,connect_timeout=2)
  28. cur = cnx.cursor()
  29. cur.execute(cmd)
  30. columns = tuple( [d[0].decode('utf8') for d in cur.description] )
  31. row = cur.fetchone()
  32. if row is None:
  33. print "not slave config"
  34. # raise StandardError("MySQL Server not configured as Slave")
  35. return (ip,port,'fail')
  36. else:
  37. result = dict(zip(columns, row))
  38. cur.close()
  39. cnx.close()
  40. return result
  41.  
  42.  
  43. def check_all_memcached():
  44. memcache_ip_pool=[]
  45. for hosts in hostlist:
  46. ip=hosts.split(':')[-2]
  47. port=int(hosts.split(':')[-1])
  48. try:
  49. ping_stats=pingcmd(ip,port)
  50. except Exception,e:
  51. print >> sys.stderr, "ping port error:",e
  52. if ping_stats[2] == "sucss":
  53. memcache_ip_pool.append(ping_stats[0])
  54. else:
  55. print "can't conn to %s: %s"%(ip,port)
  56.  
  57. if len(memcache_ip_pool) == 0:
  58. memcache_ip_pool=['0.0.0.0']
  59. return memcache_ip_pool
  60.  
  61. print check_all_memcached()
  62.  
  63.  
  64.  
  65. def check_mysql_host():
  66. mysql_ip_pool=[]
  67. config.readfp(open('/tmp/DNS.conf'))
  68. mysql_pick_list=config.get('check_mysql','ip')
  69. mysql_hostlist=map(str,mysql_pick_list.split(','))
  70. for host in mysql_hostlist:
  71. ip=host.split(':')[-2]
  72. port=int(host.split(':')[-1])
  73. try:
  74. mysql_status=runCmd("show slave status",ip,port)
  75. except MySQLdb.Error,e:
  76. print >> sys.stderr, "There was a MySQL error:",e
  77. sys.exit(1)
  78. print mysql_status[0],mysql_status[1],mysql_status[2]
  79. # if (mysql_status['Slave_IO_Running'] == 'Yes' and
  80. # mysql_status['Slave_SQL_Running'] == 'Yes' and
  81. # mysql_status['Seconds_Behind_Master'] == 0):
  82. # print "Cool"
  83. mysql_ip_pool.append(ip)
  84.  
  85. # else:
  86. # print "replication ouch... %s"%(ip,port)
  87. return mysql_ip_pool
  88.  
  89. print check_mysql_host()
Add Comment
Please, Sign In to add comment