def get_system_stats(): systemStats = { 'machine': platform.machine(), 'platform': sys.platform, 'processor': platform.processor(), 'pythonV': platform.python_version(), } platf = sys.platform if Platform.is_linux(platf): grep = subprocess.Popen(['grep', 'model name', '/proc/cpuinfo'], stdout=subprocess.PIPE, close_fds=True) wc = subprocess.Popen(['wc', '-l'], stdin=grep.stdout, stdout=subprocess.PIPE, close_fds=True) systemStats['cpuCores'] = int(wc.communicate()[0]) if Platform.is_darwin(platf): systemStats['cpuCores'] = int(subprocess.Popen(['sysctl', 'hw.ncpu'], stdout=subprocess.PIPE, close_fds=True).communicate()[0].split(': ')[1]) if Platform.is_freebsd(platf): systemStats['cpuCores'] = int(subprocess.Popen(['sysctl', 'hw.ncpu'], stdout=subprocess.PIPE, close_fds=True).communicate()[0].split(': ')[1]) if Platform.is_linux(platf): systemStats['nixV'] = platform.dist() elif Platform.is_darwin(platf): systemStats['macV'] = platform.mac_ver() elif Platform.is_freebsd(platf): version = platform.uname()[2] systemStats['fbsdV'] = ('freebsd', version, '') # no codename for FreeBSD elif Platform.is_win32(platf): systemStats['winV'] = platform.win32_ver() return systemStats
def get_system_stats(): systemStats = { "machine": platform.machine(), "platform": sys.platform, "processor": platform.processor(), "pythonV": platform.python_version(), } platf = sys.platform if Platform.is_linux(platf): grep = subprocess.Popen(["grep", "model name", "/proc/cpuinfo"], stdout=subprocess.PIPE, close_fds=True) wc = subprocess.Popen(["wc", "-l"], stdin=grep.stdout, stdout=subprocess.PIPE, close_fds=True) systemStats["cpuCores"] = int(wc.communicate()[0]) if Platform.is_darwin(platf): systemStats["cpuCores"] = int( subprocess.Popen(["sysctl", "hw.ncpu"], stdout=subprocess.PIPE, close_fds=True) .communicate()[0] .split(": ")[1] ) if Platform.is_freebsd(platf): systemStats["cpuCores"] = int( subprocess.Popen(["sysctl", "hw.ncpu"], stdout=subprocess.PIPE, close_fds=True) .communicate()[0] .split(": ")[1] ) if Platform.is_linux(platf): systemStats["nixV"] = platform.dist() elif Platform.is_darwin(platf): systemStats["macV"] = platform.mac_ver() elif Platform.is_freebsd(platf): version = platform.uname()[2] systemStats["fbsdV"] = ("freebsd", version, "") # no codename for FreeBSD elif Platform.is_win32(platf): systemStats["winV"] = platform.win32_ver() return systemStats
def parse_df_output(self, df_output, platform_name, inodes=False, use_mount=False, blacklist_re=None): """ Parse the output of the df command. If use_volume is true the volume is used to anchor the metric, otherwise false the mount point is used. Returns a tuple of (disk, inode). """ usage_data = [] # Transform the raw output into tuples of the df data. devices = self._transform_df_output(df_output, blacklist_re) # If we want to use the mount point, replace the volume name on each # line. for parts in devices: try: if use_mount: parts[0] = parts[-1] if inodes: if Platform.is_darwin(platform_name): # Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted # Inodes are in position 5, 6 and we need to compute the total # Total parts[1] = int(parts[5]) + int(parts[6]) # Total parts[2] = int(parts[5]) # Used parts[3] = int(parts[6]) # Available elif Platform.is_freebsd(platform_name): # Filesystem 1K-blocks Used Avail Capacity iused ifree %iused Mounted # Inodes are in position 5, 6 and we need to compute the total parts[1] = int(parts[5]) + int(parts[6]) # Total parts[2] = int(parts[5]) # Used parts[3] = int(parts[6]) # Available else: parts[1] = int(parts[1]) # Total parts[2] = int(parts[2]) # Used parts[3] = int(parts[3]) # Available else: parts[1] = int(parts[1]) # Total parts[2] = int(parts[2]) # Used parts[3] = int(parts[3]) # Available except IndexError: self.logger.exception("Cannot parse %s" % (parts, )) usage_data.append(parts) return usage_data
def parse_df_output(self, df_output, platform_name, inodes=False, use_mount=False, blacklist_re=None): """ Parse the output of the df command. If use_volume is true the volume is used to anchor the metric, otherwise false the mount point is used. Returns a tuple of (disk, inode). """ usage_data = [] # Transform the raw output into tuples of the df data. devices = self._transform_df_output(df_output, blacklist_re) # If we want to use the mount point, replace the volume name on each # line. for parts in devices: try: if use_mount: parts[0] = parts[-1] if inodes: if Platform.is_darwin(platform_name): # Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted # Inodes are in position 5, 6 and we need to compute the total # Total parts[1] = int(parts[5]) + int(parts[6]) # Total parts[2] = int(parts[5]) # Used parts[3] = int(parts[6]) # Available elif Platform.is_freebsd(platform_name): # Filesystem 1K-blocks Used Avail Capacity iused ifree %iused Mounted # Inodes are in position 5, 6 and we need to compute the total parts[1] = int(parts[5]) + int(parts[6]) # Total parts[2] = int(parts[5]) # Used parts[3] = int(parts[6]) # Available else: parts[1] = int(parts[1]) # Total parts[2] = int(parts[2]) # Used parts[3] = int(parts[3]) # Available else: parts[1] = int(parts[1]) # Total parts[2] = int(parts[2]) # Used parts[3] = int(parts[3]) # Available except IndexError: self.logger.exception("Cannot parse %s" % (parts,)) usage_data.append(parts) return usage_data
def _check_bsd(self, instance): netstat_flags = ['-i', '-b'] # FreeBSD's netstat truncates device names unless you pass '-W' if Platform.is_freebsd(): netstat_flags.append('-W') netstat = subprocess.Popen(["netstat"] + netstat_flags, stdout=subprocess.PIPE, close_fds=True).communicate()[0] # Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll # lo0 16384 <Link#1> 318258 0 428252203 318258 0 428252203 0 # lo0 16384 localhost fe80:1::1 318258 - 428252203 318258 - 428252203 - # lo0 16384 127 localhost 318258 - 428252203 318258 - 428252203 - # lo0 16384 localhost ::1 318258 - 428252203 318258 - 428252203 - # gif0* 1280 <Link#2> 0 0 0 0 0 0 0 # stf0* 1280 <Link#3> 0 0 0 0 0 0 0 # en0 1500 <Link#4> 04:0c:ce:db:4e:fa 20801309 0 13835457425 15149389 0 11508790198 0 # en0 1500 seneca.loca fe80:4::60c:ceff: 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 192.168.1 192.168.1.63 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # p2p0 2304 <Link#5> 06:0c:ce:db:4e:fa 0 0 0 0 0 0 0 # ham0 1404 <Link#6> 7a:79:05:4d:bf:f5 30100 0 6815204 18742 0 8494811 0 # ham0 1404 5 5.77.191.245 30100 - 6815204 18742 - 8494811 - # ham0 1404 seneca.loca fe80:6::7879:5ff: 30100 - 6815204 18742 - 8494811 - # ham0 1404 2620:9b::54 2620:9b::54d:bff5 30100 - 6815204 18742 - 8494811 - lines = netstat.split("\n") headers = lines[0].split() # Given the irregular structure of the table above, better to parse from the end of each line # Verify headers first # -7 -6 -5 -4 -3 -2 -1 for h in ("Ipkts", "Ierrs", "Ibytes", "Opkts", "Oerrs", "Obytes", "Coll"): if h not in headers: self.logger.error("%s not found in %s; cannot parse" % (h, headers)) return False current = None for l in lines[1:]: # Another header row, abort now, this is IPv6 land if "Name" in l: break x = l.split() if len(x) == 0: break iface = x[0] if iface.endswith("*"): iface = iface[:-1] if iface == current: # skip multiple lines of same interface continue else: current = iface # Filter inactive interfaces if self._parse_value(x[-5]) or self._parse_value(x[-2]): iface = current metrics = { 'bytes_rcvd': self._parse_value(x[-5]), 'bytes_sent': self._parse_value(x[-2]), 'packets_in.count': self._parse_value(x[-7]), 'packets_in.error': self._parse_value(x[-6]), 'packets_out.count': self._parse_value(x[-4]), 'packets_out.error':self._parse_value(x[-3]), } self._submit_devicemetrics(iface, metrics) netstat = subprocess.Popen(["netstat", "-s","-p" "tcp"], stdout=subprocess.PIPE, close_fds=True).communicate()[0] #3651535 packets sent # 972097 data packets (615753248 bytes) # 5009 data packets (2832232 bytes) retransmitted # 0 resends initiated by MTU discovery # 2086952 ack-only packets (471 delayed) # 0 URG only packets # 0 window probe packets # 310851 window update packets # 336829 control packets # 0 data packets sent after flow control # 3058232 checksummed in software # 3058232 segments (571218834 bytes) over IPv4 # 0 segments (0 bytes) over IPv6 #4807551 packets received # 1143534 acks (for 616095538 bytes) # 165400 duplicate acks # ... self._submit_regexed_values(netstat, BSD_TCP_METRICS)
def _check_bsd(self, instance): netstat_flags = ['-i', '-b'] # FreeBSD's netstat truncates device names unless you pass '-W' if Platform.is_freebsd(): netstat_flags.append('-W') netstat = subprocess.Popen(["netstat"] + netstat_flags, stdout=subprocess.PIPE, close_fds=True).communicate()[0] # Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll # lo0 16384 <Link#1> 318258 0 428252203 318258 0 428252203 0 # lo0 16384 localhost fe80:1::1 318258 - 428252203 318258 - 428252203 - # lo0 16384 127 localhost 318258 - 428252203 318258 - 428252203 - # lo0 16384 localhost ::1 318258 - 428252203 318258 - 428252203 - # gif0* 1280 <Link#2> 0 0 0 0 0 0 0 # stf0* 1280 <Link#3> 0 0 0 0 0 0 0 # en0 1500 <Link#4> 04:0c:ce:db:4e:fa 20801309 0 13835457425 15149389 0 11508790198 0 # en0 1500 seneca.loca fe80:4::60c:ceff: 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 192.168.1 192.168.1.63 20801309 - 13835457425 15149389 - 11508790198 - # en0 1500 2001:470:1f 2001:470:1f07:11d 20801309 - 13835457425 15149389 - 11508790198 - # p2p0 2304 <Link#5> 06:0c:ce:db:4e:fa 0 0 0 0 0 0 0 # ham0 1404 <Link#6> 7a:79:05:4d:bf:f5 30100 0 6815204 18742 0 8494811 0 # ham0 1404 5 5.77.191.245 30100 - 6815204 18742 - 8494811 - # ham0 1404 seneca.loca fe80:6::7879:5ff: 30100 - 6815204 18742 - 8494811 - # ham0 1404 2620:9b::54 2620:9b::54d:bff5 30100 - 6815204 18742 - 8494811 - lines = netstat.split("\n") headers = lines[0].split() # Given the irregular structure of the table above, better to parse from the end of each line # Verify headers first # -7 -6 -5 -4 -3 -2 -1 for h in ("Ipkts", "Ierrs", "Ibytes", "Opkts", "Oerrs", "Obytes", "Coll"): if h not in headers: self.logger.error("%s not found in %s; cannot parse" % (h, headers)) return False current = None for l in lines[1:]: # Another header row, abort now, this is IPv6 land if "Name" in l: break x = l.split() if len(x) == 0: break iface = x[0] if iface.endswith("*"): iface = iface[:-1] if iface == current: # skip multiple lines of same interface continue else: current = iface # Filter inactive interfaces if self._parse_value(x[-5]) or self._parse_value(x[-2]): iface = current metrics = { 'bytes_rcvd': self._parse_value(x[-5]), 'bytes_sent': self._parse_value(x[-2]), 'packets_in.count': self._parse_value(x[-7]), 'packets_in.error': self._parse_value(x[-6]), 'packets_out.count': self._parse_value(x[-4]), 'packets_out.error': self._parse_value(x[-3]), } self._submit_devicemetrics(iface, metrics) netstat = subprocess.Popen(["netstat", "-s", "-p" "tcp"], stdout=subprocess.PIPE, close_fds=True).communicate()[0] #3651535 packets sent # 972097 data packets (615753248 bytes) # 5009 data packets (2832232 bytes) retransmitted # 0 resends initiated by MTU discovery # 2086952 ack-only packets (471 delayed) # 0 URG only packets # 0 window probe packets # 310851 window update packets # 336829 control packets # 0 data packets sent after flow control # 3058232 checksummed in software # 3058232 segments (571218834 bytes) over IPv4 # 0 segments (0 bytes) over IPv6 #4807551 packets received # 1143534 acks (for 616095538 bytes) # 165400 duplicate acks # ... self._submit_regexed_values(netstat, BSD_TCP_METRICS)