Last active
December 19, 2015 19:48
-
-
Save rodvlopes/6008652 to your computer and use it in GitHub Desktop.
Ubuntu Unity Indicator to monitor HTTP services. Any service that returns text "ok". Read services urls from external file services.cfg. No need to restart after changing the configuration file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import gtk | |
import appindicator | |
import urllib2 | |
import re | |
import os | |
from ConfigParser import ConfigParser | |
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) | |
class Monitor: | |
def __init__(self): | |
self.update_from_config() | |
self.ind = appindicator.Indicator("new-gmail-indicator", | |
"ubuntuone-client-idle", | |
appindicator.CATEGORY_APPLICATION_STATUS) | |
self.ind.set_status(appindicator.STATUS_ACTIVE) | |
self.ind.set_attention_icon("ubuntuone-client-error") | |
self.ind.set_label("Portal Monitor") | |
self.menu_setup() | |
self.ind.set_menu(self.menu) | |
def menu_setup(self): | |
self.menu = gtk.Menu() | |
for name in self.services: | |
service_menu = gtk.MenuItem(name) | |
service_menu.show() | |
service_menu.connect("activate", self.show_dialog) | |
self.menu.append(service_menu) | |
setattr(self,'menu_'+name, service_menu) | |
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_status() | |
gtk.timeout_add(self.ping_frequency, self.check_status) | |
gtk.main() | |
def quit(self, widget): | |
sys.exit(0) | |
def check_status(self): | |
print 'check_status' | |
self.update_from_config() | |
working, result_group = self.grouped_status() | |
if working: | |
self.ind.set_status(appindicator.STATUS_ACTIVE) | |
else: | |
self.ind.set_status(appindicator.STATUS_ATTENTION) | |
self.notify_result(result_group) | |
self.update_menu(result_group) | |
return True | |
def notify_result(self, result): | |
if not result: | |
os.system('notify-send -i face-surprise "Monitor de Serviços do Portal" "Exception ao verificar serviços"') | |
return | |
message = '' | |
for service_name, service_status in result.iteritems(): | |
if not service_status['working']: | |
message += service_name + " " + "falhou. \n" | |
os.system('notify-send -i face-surprise "Monitor de Serviços do Portal"' + ' "' + message + '"') | |
def grouped_status(self): | |
ok_regexp = re.compile('.*ok.*') | |
result = {} | |
for service_name, service_url in self.services.iteritems(): | |
try: | |
service_resp = urllib2.urlopen(service_url) | |
original_response_body = service_resp.read() | |
response_body = re.sub(r'[\n\r\t]*', '', original_response_body) | |
#print service_name, response_body | |
result[service_name] = { 'working': bool(ok_regexp.match(response_body)), 'response_body': original_response_body } | |
except Exception as e: | |
print e | |
result[service_name] = { 'working': False, 'response_body': e.message } | |
finally: | |
service_resp.close() | |
print result | |
self.last_result = result | |
grouped_status_result = map(lambda i: i['working'], result.values()) | |
return not ( False in grouped_status_result ), result | |
def update_from_config(self): | |
config = ConfigParser() | |
config.read(SCRIPT_DIR + '/services.cfg') | |
self.services = dict(config.items('services')) | |
self.ping_frequency = int(dict(config.items('app'))['ping_frequency_ms']) | |
def update_menu(self, result): | |
if not result: return | |
for service_name, service_status in result.iteritems(): | |
getattr(self,'menu_'+service_name).set_label("%s: %s" % (service_name, 'OK' if service_status['working'] else 'Fora' )) | |
def show_dialog(self, widget): | |
service_name = widget.get_label().split(':')[0] | |
message = self.last_result[service_name]['response_body'] | |
dialog = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, message) | |
dialog.run() | |
dialog.destroy() | |
if __name__ == "__main__": | |
indicator = Monitor() | |
indicator.main() | |
#services.cfg example: | |
# [app] | |
# ping_frequency_ms = 30000 ;change here requires restart | |
# | |
# [services] | |
# prod-offline = http://test.com.br:8200/public/testaOffline | |
# prod-banco = http://test.com.br:8200/public/testaBanco | |
# hmg-app = http://test.com.br:8090/public/testaApp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment