monito/servers/icinga2.js

93 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-11-12 18:26:31 +01:00
/* -*- 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;
2021-11-17 21:01:51 +01:00
const Me = ExtensionUtils.getCurrentExtension();
const Preferences = Me.imports.prefs;
const GenericServer = Me.imports.servers.genericserver.GenericServer;
2021-11-12 18:26:31 +01:00
class Icinga2 extends GenericServer {
2021-12-03 16:35:50 +01:00
constructor ( _server, extension ) {
super(_server, extension, 'Icinga2');
2021-12-01 16:18:38 +01:00
this.canRecheck = false;
}
2021-11-12 18:26:31 +01:00
2021-12-03 16:35:50 +01:00
refresh ( ) {
2021-11-22 17:36:55 +01:00
this.buildURL ( );
2021-11-18 01:32:48 +01:00
this.prepareHttp ( );
}
2021-11-12 18:26:31 +01:00
2021-11-18 01:32:48 +01:00
prepareHttp ( )
{
2021-11-22 17:36:55 +01:00
super.prepareHttp ( );
2021-11-18 01:32:48 +01:00
let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi, { 'format': 'json' } );
message.request_headers.append ( 'Accept', 'application/json' );
2021-11-22 17:36:55 +01:00
this.authenticateAndSend ( message );
2021-11-18 01:32:48 +01:00
}
2021-11-12 18:26:31 +01:00
2021-11-18 01:32:48 +01:00
handleMessage ( _httpSession, message )
{
2021-11-22 17:36:55 +01:00
let _data = super.handleMessage ( _httpSession, message );
2021-11-22 22:12:49 +01:00
if ( this.error )
log ( 'Parent error ' + this.error );
else
2021-11-18 01:32:48 +01:00
{
2021-11-22 22:12:49 +01:00
let json = JSON.parse ( _data );
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
for ( var entry of json )
{
this.status.service_status.push ( {
status: _statuses [ entry.service_state ],
host_name: entry.host_name,
service_display_name: entry.service_display_name,
attempts: entry.service_attempt,
has_been_acknowledged: parseInt(entry.service_acknowledged),
2021-11-22 22:12:49 +01:00
status_information: entry.service_output,
2021-12-06 11:34:55 +01:00
last_check: this.formatDate(parseInt(entry.service_last_state_change)),
2021-11-22 22:12:49 +01:00
} );
}
2021-11-12 18:26:31 +01:00
}
2021-11-18 01:32:48 +01:00
2021-11-22 17:36:55 +01:00
this.extension.refreshUI ( this );
2021-11-25 14:45:42 +01:00
return ! this.error;
2021-11-12 18:26:31 +01:00
}
getUrlForService ( service )
{
2022-05-20 16:32:36 +02:00
log ( service );
return '%s/monitoring/service/show?host=%s&service=%s'.format ( this._settings.get_string ( 'url' ),
service.host_name,
service.service_display_name );
}
2021-11-12 18:26:31 +01:00
}