/
usr
/
local
/
lp
/
bin
/
telegraf-scripts
/
File Upload :
llllll
Current File: //usr/local/lp/bin/telegraf-scripts/ipmitool_sensors
#!/usr/bin/env python3 # PEP8 # Read metrics via "ipmitool sdr" import datetime import os class Command(object): def __init__(self, command): self.command = command def timeout(self, timeout): if os.path.isfile('/bin/timeout') and os.access('/bin/timeout', os.X_OK): self.command = '/bin/timeout %s %s' % (timeout, self.command) elif os.path.isfile('/usr/bin/timeout') and os.access('/usr/bin/timeout', os.X_OK): self.command = '/usr/bin/timeout %s %s' % (timeout, self.command) return self def run(self, shell=True): import subprocess as sp process = sp.Popen(self.command, shell=shell, stdout=sp.PIPE, stderr=sp.PIPE) self.pid = process.pid self.output, self.error = process.communicate() self.status = process.returncode return self @property def returncode(self): return self.status def transform(s): s = s.strip().lower() s = s.replace(' ', '_') return s.replace('!', '').replace('@', '').replace('#', '').replace('$', '') def is_number(s): try: float(s) return True except ValueError: return False def format(event, action, message): return 'ipmi_sensor,name={0},unit={1} value={2}'.format( transform(event), transform(action), transform(message) ) def main(): try: com = Command('/usr/bin/ipmitool -N 5 sdr').timeout(10).run() if com.error: print('unable to communicate with ipmitool.') output = [] ignorelist = ['read', '0x', 'disabled'] for line in com.output.splitlines(): # Python 3 output is type bytes, so try and convert that. try: line = str(line, "utf-8") except: pass line = line.split('|') if not any(ignore in line[1].lower() for ignore in ignorelist): name = line[0] value, unit = line[1].strip().split(' ', 1) # Influxdb will reject non-numeric and halt HH queue # https://docs.influxdata.com/enterprise_influxdb/v1.3/about-the-project/release-notes-changelog/#v1-2-5-2017-05-16 if is_number(value): output.append(format(name, unit, value)) if output: print("\n".join(output)) except Exception as e: print(e) # Either command failed or no output. pass def is_divisible_by(devisible, number): if sum(map(int, str(number))) % devisible != 0: return False return True def last_run_minute(): # return the last 'minute' run global now file = os.path.dirname(os.path.realpath(__file__)) + '/run.ipmitool_sensors' try: # Lets read the file fd_file = open(file, 'r') minute = fd_file.read() except: # exception when file does not exist, assume time 0 minute = 0 finally: # Write file fd_file = open(file, 'w+') fd_file.write("%s" % now.minute) return int(minute) if __name__ == '__main__': now = datetime.datetime.now() # Since does not allow a interval override, we'll need to do it in this script. if is_divisible_by(1, now.minute): # prevent rerun in the same minute if (last_run_minute() != now.minute): main()
Copyright ©2k19 -
Hexid
|
Tex7ure