forked from cloudkick/agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_loopback.py
67 lines (54 loc) · 1.8 KB
/
check_loopback.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
#!/usr/bin/env python
#
# Cloudkick plugin that checks if the loopback interface is available, has at least one ip address
# assigned and is up
#
import re
import sys
import random
import socket
import subprocess
DEFAULT_INTERFACE = 'lo0'
DEFAULT_TIMEOUT = 2
def main():
arg_len = len(sys.argv)
if arg_len >= 2:
loopback_interface = sys.argv[1]
else:
loopback_interface = DEFAULT_INTERFACE
if arg_len == 3:
connect_timeout = int(sys.argv[2])
else:
connect_timeout = DEFAULT_TIMEOUT
(stdout, stderr) = subprocess.Popen(['ifconfig', '-v', loopback_interface], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True).communicate()
if stderr.find('Device not found') != -1 or stderr.find('does not exist') != -1:
print 'status err %s interface not found' % (loopback_interface)
sys.exit(1)
lines = stdout.split('\n')
has_ip_address = False
for line in lines:
line = line.strip()
match = re.search(r'inet (addr)?:?(.*?) ', line)
if match:
has_ip_address = True
inet_addr = match.groups()[1]
break
if not has_ip_address:
print 'status err %s interface has no ip address' % (loopback_interface)
sys.exit(1)
port = random.randint(20000, 40000)
try:
if int(sys.version[0]) == 2 and int(sys.version[2]) <= 5:
connection_socket = socket.socket(socket.AF_INET)
connection_socket.settimeout(connect_timeout)
connection = connection_socket.connect((inet_addr, port))
else:
connection = socket.create_connection((inet_addr, port), connect_timeout)
except socket.timeout, e:
print 'status err can\'t establish connection to %s:%s' % (inet_addr, port)
sys.exit(1)
except socket.error:
pass
print 'status ok %s interface is up and working' % (loopback_interface)
sys.exit(0)
main()