diff --git a/servers/genericserver.js b/servers/genericserver.js index f94fe1a..06adc50 100644 --- a/servers/genericserver.js +++ b/servers/genericserver.js @@ -56,11 +56,11 @@ class GenericServer { this.password = this._settings.get_string ( "password" ); this.urlcgi = this._settings.get_string ( "urlcgi" ); - log ( 'monito server >>> ' + this._server ); - log ( 'monito name >>> ' + this.name ); - log ( 'monito type >>> ' + this.type ); - log ( 'monito username >>> ' + this.username ); - log ( 'monito urlcgi >>> ' + this.urlcgi ); +// log ( 'monito server >>> ' + this._server ); +// log ( 'monito name >>> ' + this.name ); +// log ( 'monito type >>> ' + this.type ); +// log ( 'monito username >>> ' + this.username ); +// log ( 'monito urlcgi >>> ' + this.urlcgi ); } @@ -91,8 +91,8 @@ class GenericServer { if ( message.status_code != Soup.Status.OK ) { - log ( '>>> error: ' + message.reason_phrase ); - log ( '>>> data: ' + message.data ); + log ( '>>> Error: ' + message.reason_phrase ); + log ( '>>> Data: ' + message.data ); Main.notifyError ( 'Monito: ' + this.name, 'URL: ' + this.urlcgi + "\n" + message.status_code + ': ' + message.reason_phrase ); @@ -113,7 +113,6 @@ class GenericServer { this.columns = Preferences.getColumns(this._server); this.sortOrder = Preferences.getSortOrder(this._server); - log ( this.sortOrder ); status = status.sort ( Lang.bind ( this, this.compareServices ) ); return status; @@ -128,6 +127,9 @@ class GenericServer { let _order = _comparison.substring ( _comparison.length - 1, _comparison.length ); if ( _name && _order && _name in a && _name in b ) { + if ( ! a [ _name ] ) + return 1; + let _result = a [ _name ] . localeCompare ( b [ _name ] ); if ( _result != 0 ) { diff --git a/servers/icinga2api.js b/servers/icinga2api.js new file mode 100644 index 0000000..8047350 --- /dev/null +++ b/servers/icinga2api.js @@ -0,0 +1,90 @@ +/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* + Monito Gnome-Shell extension + Copyright (C) 2021 Benjamin Drieu + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +const { Soup } = imports.gi; + +const ExtensionUtils = imports.misc.extensionUtils; +const Lang = imports.lang; +const Main = imports.ui.main; +const Me = ExtensionUtils.getCurrentExtension(); +const Preferences = Me.imports.prefs; +const GenericServer = Me.imports.servers.genericserver.GenericServer; + + +class Icinga2API extends GenericServer { + constructor ( _server ) { + super(_server, 'Icinga2 API'); + } + + refresh ( extension ) { + this.extension = extension; + + this.buildURL ( ); + this.prepareHttp ( ); + } + + prepareHttp ( ) + { + super.prepareHttp ( ); + + let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi, { 'format': 'json' } ); + message.request_headers.append ( 'Accept', 'application/json' ); + + this.authenticateAndSend ( message ); + } + + handleMessage ( _httpSession, message ) + { + let _data = super.handleMessage ( _httpSession, message ); + try + { + if ( this.error ) + log ( 'Parent error ' + this.error ); + else + { + let json = JSON.parse ( _data ); + let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ]; + + for ( var entry of json.results ) + { + this.status.service_status.push ( { + status: _statuses [ entry.attrs.state ], + host_name: entry.attrs.host_name, + service_display_name: entry.attrs.display_name, + attempts: '%d/%d'.format(entry.attrs.check_attempt,entry.attrs.max_check_attempts), + status_information: entry.attrs.last_check_result.output, + last_check: new Date ( entry.attrs.last_state_change * 1000 ) . toString(), + } ); + } + } + } + catch ( e ) + { + log ( '> ERROR: ' + e ); + log ( '> DATA: ' + _data ); + } + + this.extension.refreshUI ( this ); + return ! this.error; + } +} +