File: //lib/python2.7/site-packages/lap/subinterfaces.py
#!/usr/bin/python
import socket
import fcntl
import struct
import array
import glob
import re
import platform
def all_interfaces():
max_possible = 128
bytes = max_possible *32
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
names = array.array('B', '\0' * bytes)
outbytes = struct.unpack('iL', fcntl.ioctl(
s.fileno(),
0x8912, # SIOCGICONF
struct.pack('iL', bytes, names.buffer_info()[0])
))[0]
namestr = names.tostring()
lst = []
for i in range(0, outbytes, 40):
name = namestr[i:i+16].split('\0', 1)[0]
lst.append(name)
return lst
def __run__(params):
try:
dist_name = platform.dist()[0]
interfaces = all_interfaces()
ifaces_down = []
if dist_name == 'debian':
pass
elif dist_name == 'redhat':
iface_files = glob.glob('/etc/sysconfig/network-scripts/ifcfg*:*')
re_iface_name = re.compile('.*?/ifcfg-(.*?:.*)$')
re_onboot = re.compile('ONBOOT=yes', re.IGNORECASE)
ifaces_up = True
for iface_file in iface_files:
if re_iface_name.match(iface_file):
iface_match = re_iface_name.match(iface_file)
iface_name = iface_match.group(1)
# Truncate interface name
iface_name = iface_name[:15]
with open(iface_file) as fd_iface:
onboot = False
for line in fd_iface.readlines():
if re_onboot.match(line):
onboot = True
if onboot == True:
if iface_name not in interfaces:
ifaces_up = False
ifaces_down.append(iface_name)
if ifaces_up == True:
return [0,'OK']
else:
return [2,'CRITICAL: Interfaces down %s' % ifaces_down]
elif dist_name == 'Ubuntu':
iface = []
f = open("/etc/network/interfaces")
for line in f.readlines():
line = line.strip()
if "iface" in line:
iface.append(line.split()[1])
for a in iface:
if a not in interfaces:
return [2,'CRITICAL: Interfaces down %s' % a]
return [0, 'OK']
except Exception, e:
return [2,'CRITICAL: %s' % repr(e)]