Package Products :: Package DataCollector :: Package CommandParsers :: Module Linux_ifconfig
[hide private]
[frames] | no frames]

Source Code for Module Products.DataCollector.CommandParsers.Linux_ifconfig

 1  ############################################################################## 
 2  #  
 3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved. 
 4  #  
 5  # This content is made available according to terms specified in 
 6  # License.zenoss under the directory where your Zenoss product is installed. 
 7  #  
 8  ############################################################################## 
 9   
10   
11  import re 
12   
13  from CommandPlugin import CommandPlugin 
14   
15 -class Linux_ifconfig(CommandPlugin):
16 """ 17 Linux_ifconfig maps a linux ifconfig command to the interfaces relation. 18 """ 19 maptype = "InterfaceMap" 20 command = 'ifconfig' 21 compname = "os" 22 relname = "interfaces" 23 modname = "Products.ZenModel.IpInterface" 24 25 ifstart = re.compile( 26 "^(\S+)\s+Link encap:(.+)HWaddr (\S+)" 27 "|^(\S+)\s+Link encap:(.+)" 28 ).search 29 v4addr = re.compile("inet addr:(\S+).*Mask:(\S+)").search 30 flags = re.compile("^(.*) MTU:(\d+)\s+Metric:.*").search 31 32
33 - def condition(self, device, log):
34 osver = device.os.getProductName() 35 return osver.find("Linux") > -1
36 37
38 - def process(self, device, results, log):
39 log.info('Collecting interfaces for device %s' % device.id) 40 rm = self.relMap() 41 rlines = results.split("\n") 42 for line in rlines: 43 m = self.ifstart(line) 44 if m: 45 # start new interface and get name, type, and macaddress 46 iface = self.objectMap() 47 rm.append(iface) 48 if m.lastindex == 3: 49 name, itype, iface.macaddress=m.groups()[:3] 50 else: 51 name, itype = m.groups()[3:] 52 iface.type = itype.strip() 53 # Left iface.name in place, but added 54 # iface.title for consistency 55 iface.title = name 56 iface.name = name 57 iface.id = self.prepId(name) 58 continue 59 m = self.v4addr(line) 60 if m: 61 # get ip and netmask 62 ip, netmask = m.groups() 63 netmask = self.maskToBits(netmask) 64 iface.setIpAddresses = ["%s/%s" % (ip, netmask)] 65 m = self.flags(line) 66 if m: 67 # get adminStatus, operStatus, and mtu 68 flags, mtu = m.groups() 69 if "UP" in flags: iface.operStatus = 1 70 else: iface.operStatus = 2 71 if "RUNNING" in flags: iface.adminStatus = 1 72 else: iface.adminStatus = 2 73 iface.mtu = int(mtu) 74 return rm
75