From fac892415007d4b2025b15361ba70c3e81cffb29 Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Sat, 30 Nov 2013 15:26:25 +0400 Subject: [PATCH] =?utf8?q?=D0=98=D0=BD=D0=B4=D0=B8=D0=BA=D0=B0=D1=82=D0=BE?= =?utf8?q?=D1=80=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=B8=D1=81=D1=82=D0=B5?= =?utf8?q?=D0=BC=D0=BD=D0=BE=D0=B9=20=D1=81=D0=BB=D1=83=D0=B6=D0=B1=D1=8B?= =?utf8?q?=20NUT?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- indicator-nut.py | 173 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100755 indicator-nut.py diff --git a/indicator-nut.py b/indicator-nut.py new file mode 100755 index 0000000..1fd48e9 --- /dev/null +++ b/indicator-nut.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python + +import sys +import gtk +import appindicator + +import imaplib +import re + +import os +import pynotify + +PING_FREQUENCY = 10 # seconds + +def exec_command(command): + stream = os.popen(command) + out = stream.read() + out = re.split('\n',out); + res = [] + for newline in out: + if len(newline)==0: + continue; + elif newline.startswith("Error:"): + res=[]; + break; + else: + res.append(newline) + return res + +class CheckNUT: + + def __init__(self): + self.ind = appindicator.Indicator("nut-indicator", + "gpm-ups-100-charging", + appindicator.CATEGORY_APPLICATION_STATUS) + self.ind.set_status(appindicator.STATUS_ACTIVE) + self.ind.set_attention_icon("gpm-ups-000") + + hosts = sys.argv[1:] + + if len(hosts)==0: + hosts = {'localhost'} + + self.upses=[] + for host in hosts : + command = "upsc -l {0}".format(host) + try: + local_upses=exec_command(command) + for ups in local_upses: + self.upses.append('{0}@{1}'.format(ups,host)) + except: + pass + + if self.upses == []: + print "No connected ups'es found!\n" + sys.exit(1) + + self.menu_setup() + self.ind.set_menu(self.menu) + + if not pynotify.init ("nut-indicator"): + self.disable_notify=1 + else: + self.disable_notify=0 + + self.ups_dictionary = {} + self.ups_dictionary['battery charge'] = 'Battery charge %' + self.ups_dictionary['input voltage'] = 'Input voltage' + self.ups_dictionary['output voltage'] = 'Output voltage' + self.ups_dictionary['ups load'] = 'UPS load %' + self.ups_dictionary['ups status'] = 'UPS status' + + self.ups_value_dictionary = {} + self.ups_value_dictionary['ups status'] = {} + self.ups_value_dictionary['ups status']['OL'] = {1,'online'} + self.ups_value_dictionary['ups status']['OB'] = {0,'on battery'} + self.ups_value_dictionary['ups status']['LB'] = {-1,'low batter'} + + def menu_setup(self): + self.menu = gtk.Menu() + + self.menuitems = {} + for ups in self.upses: + print ups; + menu_item = gtk.MenuItem(ups) + menu_item.connect("activate",self.call_check_local_ups,ups) + menu_item.show() + self.menu.append(menu_item) + self.menuitems[ups]=menu_item + + separator = gtk.SeparatorMenuItem() + separator.show() + self.menu.append(separator) + + self.quit_item = gtk.MenuItem("Quit") + self.quit_item.connect("activate", self.quit) + self.quit_item.show() + self.menu.append(self.quit_item) + + def main(self): + self.check_ups() + gtk.timeout_add(PING_FREQUENCY * 1000, self.check_ups) + gtk.main() + + def quit(self, widget): + sys.exit(0) + + def call_check_local_ups(self,item,ups): + return self.check_local_ups(ups,1) + + def check_local_ups(self,ups,notify): + command = "upsc {0}".format(ups) + res = exec_command(command) + status="?" + battery="?" + ups_status="?" + text="" + minstatus=1 + for newline in res: + paramname,paramvalue = newline.split(': ',1) + paramname = paramname.replace('.',' ') + try: + status,paramvalue = self.ups_value_dictionary[paramname][paramvalue] + if status