├── src ├── __init__.py ├── icon.png ├── config_parser.py ├── newrelic_app.py ├── newrelic_indicator.py └── config_window.py ├── .gitignore ├── run_tests.sh ├── config.json ├── newrelic-indicator.sh ├── Makefile ├── main.py ├── tests.py ├── .travis.yml ├── tests ├── config_parser_test.py ├── newrelic_indicator_test.py └── newrelic_app_test.py ├── register-startup.sh └── README.md /src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | pyrg -v tests.py 3 | -------------------------------------------------------------------------------- /src/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zizaco/newrelic-indicator/HEAD/src/icon.png -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "API Key": "00000000000000000000000000000000000000000000000", 3 | "App ID": "0000000" 4 | } 5 | -------------------------------------------------------------------------------- /newrelic-indicator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | nohup $DIR/main.py > output.log 2>&1& 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean 2 | prepare: 3 | apt-get update -qq 4 | apt-get install git libgtk2.0-dev libglib2.0-dev 5 | apt-get install python-appindicator 6 | 7 | install: 8 | ./register-startup.sh 9 | ./newrelic-indicator.sh 10 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from src.newrelic_indicator import * 4 | from src.newrelic_app import * 5 | from src.config_parser import * 6 | 7 | if __name__ == "__main__": 8 | config = ConfigParser() 9 | config.read('config.json') 10 | 11 | app = NewrelicApp(config) 12 | 13 | indicator = NewrelicIndicator(config) 14 | indicator.set_app(app) 15 | indicator.run() 16 | -------------------------------------------------------------------------------- /tests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import os 5 | import glob 6 | import unittest 7 | from mock import Mock 8 | 9 | # Mocks Module dependencies 10 | sys.modules['gtk'] = Mock(name='gtk_module') 11 | sys.modules['appindicator'] = Mock(name='appindicator_module') 12 | 13 | # Import src modules 14 | from src.newrelic_indicator import * 15 | from src.newrelic_app import * 16 | from src.config_parser import * 17 | 18 | # Load test files 19 | test_files = glob.glob("tests/"+os.path.dirname(__file__)+"/*.py") 20 | 21 | for files in test_files: 22 | execfile(files) 23 | 24 | # Run test 25 | if __name__ == '__main__': 26 | unittest.main() 27 | 28 | -------------------------------------------------------------------------------- /src/config_parser.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | 4 | class ConfigParser: 5 | 6 | raw_config = {} 7 | filename = "" 8 | 9 | def __init__(self): 10 | self.path = os.path.dirname(os.path.realpath(__file__)) 11 | 12 | def read(self, filename): 13 | self.filename = filename 14 | self.raw_config = json.load(open(self.path+'/../'+self.filename)) 15 | 16 | return self.raw_config 17 | 18 | def get_value(self, key): 19 | return self.raw_config[key] if (key in self.raw_config) else False 20 | 21 | def set_value(self, key, value): 22 | self.raw_config[key] = value 23 | 24 | def persist(self): 25 | with open(self.path+'/../'+self.filename, "w") as outfile: 26 | json.dump(self.raw_config, outfile, indent=4) 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "2.7" 5 | 6 | before_install: 7 | - sudo apt-get update -qq 8 | - sudo apt-get install git libgtk2.0-dev libglib2.0-dev 9 | 10 | install: 11 | # Environment setup 12 | - export VIRT_ROOT=/home/travis/virtualenv/python$TRAVIS_PYTHON_VERSION 13 | - export PKG_CONFIG_PATH=$VIRT_ROOT/lib/pkgconfig 14 | # PyGobject 15 | - wget http://ftp.gnome.org/pub/GNOME/sources/pygobject/2.28/pygobject-2.28.6.tar.bz2 16 | - tar xf pygobject-2.28.6.tar.bz2 17 | - cd pygobject-2.28.6 18 | - ./configure --prefix=$VIRT_ROOT --disable-introspection > /dev/null 19 | - make > /dev/null 20 | - sudo make install > /dev/null 21 | - cd .. 22 | # PyGtk 23 | - wget http://ftp.gnome.org/pub/GNOME/sources/pygtk/2.24/pygtk-2.24.0.tar.bz2 24 | - tar xf pygtk-2.24.0.tar.bz2 25 | - cd pygtk-2.24.0 26 | - ./configure --prefix=$VIRT_ROOT > /dev/null 27 | - make > /dev/null 28 | - sudo make install > /dev/null 29 | - cd .. 30 | # Appindicator 31 | - sudo apt-get install python-appindicator 32 | # Mock 33 | - pip install mock 34 | # Pyrg 35 | - easy_install pyrg 36 | 37 | script: ./run_tests.sh 38 | -------------------------------------------------------------------------------- /tests/config_parser_test.py: -------------------------------------------------------------------------------- 1 | from mock import Mock 2 | 3 | class ConfigParserTest(unittest.TestCase): 4 | 5 | def test_should_instantiate(self): 6 | # Set 7 | conf_parser = ConfigParser() 8 | 9 | # Assertion 10 | self.assertIsInstance(conf_parser, ConfigParser) 11 | self.assertIn('src', conf_parser.path) 12 | 13 | def test_should_read(self): 14 | # Set 15 | conf_parser = ConfigParser() 16 | config_file_content = {'API Key': '1111', 'App ID': '777'} 17 | 18 | # Mocks 19 | json.load = Mock(return_value=config_file_content) 20 | 21 | # Assertion 22 | self.assertEqual(config_file_content, conf_parser.read('config.json')) 23 | self.assertEqual(config_file_content, conf_parser.raw_config) 24 | json.load.assert_called_once() 25 | 26 | def test_should_get_value(self): 27 | # Set 28 | conf_parser = ConfigParser() 29 | conf_parser.raw_config = {'API Key': '1111', 'App ID': '777'} 30 | 31 | # Assertion 32 | self.assertEqual('1111', conf_parser.get_value('API Key')) 33 | self.assertEqual('777', conf_parser.get_value('App ID')) 34 | self.assertFalse(conf_parser.get_value('Undefined')) 35 | -------------------------------------------------------------------------------- /register-startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | while true; do 4 | read -p "Do you wish to run Newrelic Indicator at startup? [yn] " yn 5 | case $yn in 6 | [Yy]* ) 7 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 8 | DESKTOP_FILE="/etc/xdg/autostart/indicator-newrelic.desktop" 9 | sudo touch $DESKTOP_FILE 10 | sudo chmod 777 $DESKTOP_FILE 11 | sudo echo "[Desktop Entry]" > $DESKTOP_FILE 12 | sudo echo "Type=Application" >> $DESKTOP_FILE 13 | sudo echo "Exec=$DIR/main.py" >> $DESKTOP_FILE 14 | sudo echo "Hidden=false" >> $DESKTOP_FILE 15 | sudo echo "NoDisplay=false" >> $DESKTOP_FILE 16 | sudo echo "OnlyShowIn=Unity;XFCE;GNOME;" >> $DESKTOP_FILE 17 | sudo echo "AutostartCondition=GNOME3 unless-session gnome" >> $DESKTOP_FILE 18 | sudo echo "Name=Newrelic Indicator" >> $DESKTOP_FILE 19 | sudo echo "Comment=Indicator to monitor metrics from newrelic for Unity" >> $DESKTOP_FILE 20 | sudo chmod 644 $DESKTOP_FILE 21 | echo "..." 22 | echo "Added Newrelic Indicator to system startup" 23 | break;; 24 | [Nn]* ) exit;; 25 | * ) echo "Please answer yes or no.";; 26 | esac 27 | done 28 | 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NewRelic Indicator Applet 2 | 3 | _A indicator applet to monitor metrics from newrelic for [Ubuntu's Unity Desktop](https://unity.ubuntu.com/)_ 4 | 5 | [![Build Status](https://travis-ci.org/Zizaco/newrelic-indicator.svg?branch=master)](https://travis-ci.org/Zizaco/newrelic-indicator) 6 | [![License MIT](http://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT) 7 | 8 | ![NewRelic Indicator Screenshot](https://cloud.githubusercontent.com/assets/777635/25009105/ef94a794-203b-11e7-9745-c9720d94fc9e.png) 9 | 10 | ## Quick start 11 | 12 | **1.** Download files 13 | 14 | $ git clone git@github.com:Zizaco/newrelic-indicator.git .newrelic-indicator 15 | $ cd .newrelic-indicator 16 | 17 | **2.** Install the dependencies 18 | 19 | $ sudo make 20 | 21 | **3.** Add the indicator to run at startup. 22 | 23 | $ make install 24 | Do you wish to run Newrelic Indicator at startup? [yn] y 25 | ... 26 | Added Newrelic Indicator to system startup 27 | 28 | **4.** Set your **API Key** and your **App ID** 29 | 30 | ![newrelicindicatorconfig](https://cloud.githubusercontent.com/assets/777635/25074459/7a181918-22d1-11e7-8fdb-4dfbb8265930.png) 31 | 32 | ## Troubleshooting 33 | 34 | You may need to install **Appindicator** in order to run the Indicator properly: 35 | 36 | sudo apt-get install python-appindicator 37 | 38 | Any execution error will be written to `output.log`. 39 | 40 | ## License 41 | 42 | Newrelic Indicator Applet is free software distributed under the terms of the [MIT license](http://opensource.org/licenses/MIT) 43 | 44 | ## Aditional information 45 | 46 | Any questions, feel free to contact me 47 | 48 | Any issues, please [report here](https://github.com/Zizaco/newrelic-indicator/issues) 49 | -------------------------------------------------------------------------------- /src/newrelic_app.py: -------------------------------------------------------------------------------- 1 | import json 2 | import httplib 3 | 4 | class NewrelicApp: 5 | 6 | def __init__(self, config_parser): 7 | self.config_parser = config_parser 8 | self.raw_app = {} 9 | self.reachable = False 10 | 11 | def update_state(self): 12 | headers = {"X-Api-Key":self.config_parser.get_value("API Key")} 13 | url ='/v2/applications/'+self.config_parser.get_value("App ID")+'.json' 14 | conn = httplib.HTTPConnection('api.newrelic.com') 15 | 16 | try: 17 | conn.connect() 18 | conn.request('GET', url, '', headers) 19 | response = conn.getresponse() 20 | if response.status == 200: 21 | self.raw_app = json.loads(response.read()) 22 | self.reachable = True 23 | return True 24 | except: 25 | print "Unable to connect" 26 | 27 | self.reachable = False 28 | return False 29 | 30 | def is_online(self): 31 | return self.reachable 32 | 33 | def get_rpm(self): 34 | if self.is_online(): 35 | throughput = self.raw_app['application']['application_summary']['throughput'] 36 | else: 37 | throughput = False 38 | 39 | if throughput > 1000: 40 | throughput = "{0:.1f}".format(throughput*0.001)+"k " 41 | 42 | return throughput 43 | 44 | def get_response_time(self): 45 | return self.raw_app['application']['application_summary']['response_time'] if self.is_online() else False 46 | 47 | def get_errors(self): 48 | return self.raw_app['application']['application_summary']['error_rate'] if self.is_online() else False 49 | 50 | def get_info(self): 51 | return ("{0:.0f}".format(self.get_response_time())+"ms | "+str(self.get_rpm())+"rpm | "+"{0:.1f}".format(self.get_errors()*100)+"% errors") if self.is_online() else "info unavailable" 52 | 53 | -------------------------------------------------------------------------------- /src/newrelic_indicator.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import gtk 4 | import appindicator 5 | from src.config_window import * 6 | 7 | class NewrelicIndicator: 8 | 9 | ping_frequency = 60 # seconds 10 | 11 | def __init__(self, config_parser): 12 | self.config_parser = config_parser 13 | self.setup() 14 | self.build_menu() 15 | 16 | def setup(self): 17 | self.path = os.path.dirname(os.path.realpath(__file__)) 18 | self.indicator = appindicator.Indicator("newrelic-indicator", self.path + "/icon.png", appindicator.CATEGORY_APPLICATION_STATUS) 19 | self.indicator.set_status(appindicator.STATUS_ACTIVE) 20 | self.indicator.set_label("New Relic") 21 | 22 | def build_menu(self): 23 | self.__add_menu_iten('Configuration', self.show_configs) 24 | self.__add_menu_iten('Quit', self.quit) 25 | 26 | def __add_menu_iten(self, name, method): 27 | if not hasattr(self, 'menu'): 28 | self.menu = gtk.Menu() 29 | self.indicator.set_menu(self.menu) 30 | 31 | item = gtk.MenuItem(name) 32 | item.connect("activate", method) 33 | item.show() 34 | self.menu.append(item) 35 | 36 | def run(self): 37 | self.refresh() 38 | gtk.timeout_add(self.ping_frequency * 1000, self.refresh) 39 | gtk.main() 40 | 41 | def refresh(self): 42 | if hasattr(self, 'newrelic_app'): 43 | self.newrelic_app.update_state() 44 | self.indicator.set_label(self.newrelic_app.get_info()) 45 | 46 | return True 47 | 48 | def set_app(self, newrelic_app): 49 | self.newrelic_app = newrelic_app 50 | 51 | def quit(self, widget): 52 | sys.exit(0) 53 | 54 | def show_configs(self, widget): 55 | if not hasattr(self, 'config_window'): 56 | self.config_window = ConfigWindow(self, self.config_parser) 57 | 58 | self.config_window.open() 59 | -------------------------------------------------------------------------------- /tests/newrelic_indicator_test.py: -------------------------------------------------------------------------------- 1 | from mock import Mock 2 | 3 | class NewrelicIndicatorTest(unittest.TestCase): 4 | 5 | def setUp(self): 6 | # Set 7 | self.original_setup = NewrelicIndicator.setup 8 | self.original_build_menu = NewrelicIndicator.build_menu 9 | 10 | # Mocks 11 | NewrelicIndicator.setup = Mock() 12 | NewrelicIndicator.build_menu = Mock() 13 | 14 | def tearDown(self): 15 | # Restore original class methods 16 | if hasattr(self, 'original_setup'): 17 | NewrelicIndicator.setup = self.original_setup 18 | 19 | if hasattr(self, 'original_build_menu'): 20 | NewrelicIndicator.build_menu = self.original_build_menu 21 | 22 | def test_should_instantiate(self): 23 | # Assertion 24 | ind = NewrelicIndicator() 25 | 26 | self.assertIsInstance(ind, NewrelicIndicator) 27 | assert NewrelicIndicator.setup.called 28 | assert NewrelicIndicator.build_menu.called 29 | 30 | def test_should_setup(self): 31 | # Set 32 | ind = NewrelicIndicator() 33 | NewrelicIndicator.setup = self.original_setup 34 | 35 | # Mocks 36 | indicator_obj = Mock(name='Indicator') 37 | indicator_obj.set_status = Mock() 38 | indicator_obj.set_label = Mock() 39 | 40 | appindicator.Indicator = Mock(return_value=indicator_obj) 41 | appindicator.STATUS_ACTIVE = Mock() 42 | 43 | # Assertion 44 | ind.setup() 45 | self.assertIn('src', ind.path) 46 | self.assertEquals(ind.indicator, indicator_obj) 47 | appindicator.Indicator.assert_called_once_with("newrelic-indicator", ind.path + "/icon.png", appindicator.CATEGORY_APPLICATION_STATUS) 48 | indicator_obj.set_status.assert_called_once_with(appindicator.STATUS_ACTIVE) 49 | indicator_obj.set_label.assert_called_once_with("New Relic") 50 | 51 | def test_should_build_menu(self): 52 | #Set 53 | ind = NewrelicIndicator() 54 | NewrelicIndicator.build_menu = self.original_build_menu 55 | 56 | # Mocks 57 | ind._NewrelicIndicator__add_menu_iten = Mock() 58 | 59 | # Assertion 60 | ind.build_menu() 61 | 62 | ind._NewrelicIndicator__add_menu_iten.assert_called_once_with('Quit', ind.quit) 63 | 64 | def test_should_add_menu_iten(self): 65 | # Set 66 | ind = NewrelicIndicator() 67 | 68 | # Mocks 69 | method_callback = Mock(name='Method') 70 | menu_obj = Mock(name='Menu') 71 | menu_obj.append = Mock() 72 | menu_iten_obj = Mock(name='MenuItem') 73 | menu_iten_obj.connect = Mock() 74 | menu_iten_obj.show = Mock() 75 | ind.indicator = Mock(name='Indicator') 76 | ind.indicator.set_menu = Mock() 77 | 78 | gtk.Menu = Mock(return_value=menu_obj) 79 | gtk.MenuItem = Mock(return_value=menu_iten_obj) 80 | 81 | # Assertion 82 | ind._NewrelicIndicator__add_menu_iten('ItemName', method_callback) 83 | 84 | gtk.MenuItem.assert_called_once_with('ItemName') 85 | 86 | self.assertEquals(ind.menu, menu_obj) 87 | menu_iten_obj.connect.assert_called_once_with("activate", method_callback) 88 | menu_iten_obj.show.assert_called_once() 89 | menu_obj.append.assert_called_once_with(menu_iten_obj) 90 | ind.indicator.set_menu.assert_called_once_with(menu_obj) 91 | 92 | -------------------------------------------------------------------------------- /src/config_window.py: -------------------------------------------------------------------------------- 1 | import gtk 2 | from src.config_parser import * 3 | 4 | class ConfigWindow: 5 | 6 | current_tr = 0 7 | current_td = 0 8 | 9 | def __init__(self, parent, config_parser): 10 | self.parent = parent 11 | self.config_parser = config_parser 12 | self.setup() 13 | 14 | def setup(self): 15 | self.window = gtk.Window() 16 | self.window.set_title("NewRelic Indicator Configuration") 17 | self.window.set_border_width(15) 18 | 19 | self.box = gtk.Table(4, 3, True) 20 | self.window.add(self.box) 21 | 22 | self.prepare_window_elements() 23 | self.assemble_window_elements() 24 | 25 | def prepare_window_elements(self): 26 | self.label_api_key = gtk.Label('API Key') 27 | self.label_api_key_help = gtk.Label() 28 | self.label_api_key_help.set_markup('NewRelic\'s API Key of your account') 29 | self.text_api_key = gtk.Entry() 30 | self.label_api_id = gtk.Label('API ID') 31 | self.label_api_id_help = gtk.Label() 32 | self.label_api_id_help.set_markup('The number in your app url at NewRelic') 33 | self.text_api_id = gtk.Entry() 34 | self.btn_close = gtk.Button(stock=gtk.STOCK_CLOSE) 35 | self.btn_save = gtk.Button(stock=gtk.STOCK_APPLY) 36 | 37 | def assemble_window_elements(self): 38 | self.add_element(self.label_api_key) 39 | self.add_element(self.text_api_key, 2) 40 | self.line_break() 41 | self.skip() 42 | self.add_element(self.label_api_key_help, 2) 43 | self.line_break() 44 | self.add_element(self.label_api_id) 45 | self.add_element(self.text_api_id, 2) 46 | self.line_break() 47 | self.skip() 48 | self.add_element(self.label_api_id_help, 2) 49 | self.line_break() 50 | self.add_element(gtk.HSeparator(), 3) 51 | self.line_break() 52 | self.skip() 53 | self.add_element(self.btn_save) 54 | self.add_element(self.btn_close) 55 | 56 | self.registerEvents() 57 | 58 | def skip(self, size=1): 59 | self.current_td += size 60 | 61 | def line_break(self): 62 | self.current_td = 0 63 | self.current_tr += 1 64 | 65 | def add_element(self, element, size=1): 66 | self.box.attach(element, self.current_td, self.current_td+size, self.current_tr, self.current_tr+1) 67 | self.current_td += size 68 | element.show() 69 | 70 | def open(self): 71 | self.box.show() 72 | self.populate_fields() 73 | self.window.set_position(gtk.WIN_POS_CENTER) 74 | self.window.show() 75 | self.window.set_keep_above(True) 76 | 77 | def registerEvents(self): 78 | self.btn_close.connect('clicked', self.close, None) 79 | self.btn_save.connect('clicked', self.persist_fields, None) 80 | self.window.connect("delete_event", self.close, None) 81 | 82 | def close(self, widget, data=None, foo=None): 83 | self.window.hide() 84 | return True 85 | 86 | def populate_fields(self): 87 | self.text_api_key.set_text(self.config_parser.get_value("API Key")) 88 | self.text_api_id.set_text(self.config_parser.get_value("App ID")) 89 | 90 | def persist_fields(self, widget, data=None): 91 | self.config_parser.set_value("API Key", self.text_api_key.get_text()) 92 | self.config_parser.set_value("App ID", self.text_api_id.get_text()) 93 | self.config_parser.persist() 94 | self.parent.refresh() 95 | 96 | md = gtk.MessageDialog(self.window, 97 | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, 98 | gtk.BUTTONS_CLOSE, "Settings have been updated.") 99 | md.run() 100 | md.destroy() 101 | 102 | -------------------------------------------------------------------------------- /tests/newrelic_app_test.py: -------------------------------------------------------------------------------- 1 | from mock import Mock 2 | 3 | class NewrelicAppTest(unittest.TestCase): 4 | 5 | def test_should_instantiate(self): 6 | # Set 7 | app = NewrelicApp('1111','777') 8 | 9 | # Assertion 10 | self.assertIsInstance(app, NewrelicApp) 11 | self.assertEquals(app.api_key,'1111') 12 | self.assertEquals(app.api_id,'777') 13 | self.assertFalse(app.reachable) 14 | 15 | def test_should_update_state_if_available(self): 16 | # Set 17 | app = NewrelicApp('1111','777') 18 | headers = {"X-Api-Key":'1111'} 19 | url ='/v2/applications/777.json' 20 | 21 | # Mocks 22 | connection_obj = Mock() 23 | response_obj = Mock() 24 | httplib.HTTPConnection = Mock(return_value=connection_obj) 25 | 26 | response_obj.status = 200 27 | response_obj.read = Mock(return_value='{"some":"json"}') 28 | connection_obj.connect = Mock() 29 | connection_obj.request = Mock() 30 | connection_obj.getresponse = Mock(return_value=response_obj) 31 | 32 | # Assertion 33 | app.update_state() 34 | 35 | connection_obj.connect.assert_called_once() 36 | connection_obj.request.assert_called_once_with('GET', url, '', headers) 37 | connection_obj.getresponse.assert_called_once() 38 | 39 | self.assertEquals(app.raw_app, {"some":"json"}) 40 | self.assertTrue(app.reachable) 41 | 42 | def test_should_not_update_state_if_unaavailable(self): 43 | # Set 44 | app = NewrelicApp('1111','777') 45 | headers = {"X-Api-Key":'1111'} 46 | url ='/v2/applications/777.json' 47 | 48 | # Mocks 49 | connection_obj = Mock() 50 | response_obj = Mock() 51 | httplib.HTTPConnection = Mock(return_value=connection_obj) 52 | 53 | response_obj.status = 500 # Error 54 | response_obj.read = Mock(return_value='') 55 | connection_obj.connect = Mock() 56 | connection_obj.request = Mock() 57 | connection_obj.getresponse = Mock(return_value=response_obj) 58 | 59 | # Assertion 60 | app.update_state() 61 | 62 | connection_obj.connect.assert_called_once() 63 | connection_obj.request.assert_called_once_with('GET', url, '', headers) 64 | connection_obj.getresponse.assert_called_once() 65 | 66 | self.assertFalse(app.reachable) 67 | 68 | def test_should_check_is_online(self): 69 | # Set 70 | app = NewrelicApp('1111','777') 71 | 72 | # Assert 73 | app.reachable = True 74 | self.assertTrue(app.is_online()) 75 | 76 | app.reachable = False 77 | self.assertFalse(app.is_online()) 78 | 79 | def test_should_get_rpm(self): 80 | # Set 81 | app = NewrelicApp('1111','777') 82 | app.raw_app = {'application':{'application_summary':{'throughput':300}}} 83 | 84 | # Assert 85 | app.reachable = True 86 | self.assertEquals(300, app.get_rpm()) 87 | 88 | app.reachable = False 89 | self.assertFalse(app.get_rpm()) 90 | 91 | def test_should_get_rpm_greater_than_1k(self): 92 | # Set 93 | app = NewrelicApp('1111','777') 94 | app.raw_app = {'application':{'application_summary':{'throughput':3000}}} 95 | 96 | # Assert 97 | app.reachable = True 98 | self.assertEquals('3.0k ', app.get_rpm()) 99 | 100 | app.reachable = False 101 | self.assertFalse(app.get_rpm()) 102 | 103 | def test_should_get_response_time(self): 104 | # Set 105 | app = NewrelicApp('1111','777') 106 | app.raw_app = {'application':{'application_summary':{'response_time':100}}} 107 | 108 | # Assert 109 | app.reachable = True 110 | self.assertEquals(100, app.get_response_time()) 111 | 112 | app.reachable = False 113 | self.assertFalse(app.get_response_time()) 114 | 115 | def test_should_get_errors(self): 116 | # Set 117 | app = NewrelicApp('1111','777') 118 | app.raw_app = {'application':{'application_summary':{'error_rate':3}}} 119 | 120 | # Assert 121 | app.reachable = True 122 | self.assertEquals(3, app.get_errors()) 123 | 124 | app.reachable = False 125 | self.assertFalse(app.get_errors()) 126 | 127 | def test_should_get_info(self): 128 | # Set 129 | app = NewrelicApp('1111','777') 130 | 131 | # Mocks 132 | app.get_rpm = Mock(return_value='300') 133 | app.get_response_time = Mock(return_value=85.9) 134 | app.get_errors = Mock(return_value=0.50) 135 | 136 | # Asset 137 | app.reachable = True 138 | self.assertEquals('86ms | 300rpm | 50.0% errors', app.get_info()) 139 | 140 | app.reachable = False 141 | self.assertEquals('info unavailable', app.get_info()) 142 | --------------------------------------------------------------------------------