-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ip.py
137 lines (122 loc) · 4.05 KB
/
check_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/env python
# --*-- coding:utf-8 --*--
#取数据库里所有ip,做fping操作,判断返回值是否为alive,否则触发报警,支持恢复操作
import os
from os import system
import urllib2
import MySQLdb
import commands
from commands import getoutput
import sys
import subprocess
import yaml
import socket
import time
import urllib
import urllib2
import json
name_list = []
name_lists = []
def confload():
global conf
conf_path = '/opt/rh/iplist.conf'
if os.path.exists(conf_path):
conf = yaml.load(open(conf_path))
def get_count(group):
confload()
for i in conf.get('ip_conf'):
m = []
m = i.get('%s' % group,None)
return m
def get_list():
conn = MySQLdb.connect(host='10.80.2.156',user='open',passwd='jjker1314',db='ass',charset='utf8')
cursor = conn.cursor()
count = cursor.execute("select server_wai_ip from server_info where server_wai_ip <> '';")
ip_list = []
results = cursor.fetchall()
result=list(results)
for r in result:
ip_list.append(('%s' % r))
return ip_list
conn.close()
def get_info(info):
conn = MySQLdb.connect(host='10.80.2.156',user='open',passwd='jjker1314',db='ass',charset='utf8')
cursor = conn.cursor()
count = cursor.execute("select server_name from server_info where server_wai_ip = '%s';" % info)
ip_list = []
results = cursor.fetchall()
result=list(results)
for r in result:
ip_list.append(('%s' % r))
return ", ".join(ip_list)
conn.close()
def iplist():
set1 = set(get_count('ip'))
set2 = set(get_list())
return list(set1 ^ set2) + get_count('LB')
def alarm(ip,la):
post_url = 'http://ass.51.com/sms/sms1.php'
postData = {'problem':'from BJ-174 Network Abnormal','status':'%s' % la,'host':'%s::%s' % (get_info(ip),ip)}
req = urllib2.Request(post_url)
response = urllib2.urlopen(req,urllib.urlencode(postData))
print response.read()
def files(ip):
f = file("/opt/rh/iplist.txt","a+")
f.writelines("%s\n" % ip)
f.close()
def ofo():
mylist = []
mylist = getoutput("cat /opt/rh/iplist.txt")
mylists = mylist.split('\n')
myset = list(set(mylists)) #myset是另外一个列表,里面的内容是mylist里面的无重复 项
for item in myset:
if int(mylist.count(item)) == int(1):
print("the %s has found %s" %(item,mylist.count(item)))
alarm(item,'problem')
else:
print("the %s has founds %s" %(item,mylist.count(item)))
def run():
for i in iplist():
met = getoutput("/usr/local/sbin/fping %s | awk '{print $1}'" % i)
ret = getoutput("/usr/local/sbin/fping %s | awk '{print $3}'" % i)
if ret != 'alive':
name_list.append(('%s' % met))
else:
print "ip-%s status is %s" % (met,ret)
def runer():
run()
time.sleep(10)
for i in name_list:
met = getoutput("/usr/local/sbin/fping %s | awk '{print $1}'" % i)
ret = getoutput("/usr/local/sbin/fping %s | awk '{print $3}'" % i)
if ret != 'alive':
print "ip-%s status is %s" % (met,ret)
files(i)
else:
print "ip-%s status is %s" % (met,ret)
def runers():
runer()
met = []
met = getoutput("cat /opt/rh/iplist.txt")
if met != "":
ofo()
mes = met.split('\n')
res = list(set(mes))
for i in res:
ret = getoutput("/usr/local/sbin/fping %s | awk '{print $3}'" % i)
if ret == 'alive':
alarm(i,"OK")
os.system("rm -rf /opt/rh/iplist.txt")
else:
print "ip-%s status3 is %s" % (i,ret)
else:
print "file is empty!"
if __name__ == '__main__':
try:
if os.path.exists("/opt/rh/iplist.txt"):
runers()
else:
system("touch /opt/rh/iplist.txt")
runers()
except Exception,e:
print e