112 lines
3.9 KiB
JavaScript
112 lines
3.9 KiB
JavaScript
/* -*- 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;
|
|
|
|
let _httpSession;
|
|
|
|
|
|
class Icinga2 extends GenericServer {
|
|
constructor ( _server ) {
|
|
super(_server, 'Icinga2');
|
|
}
|
|
|
|
refresh ( extension ) {
|
|
this.extension = extension;
|
|
this.settings = Preferences.getAccountSettings ( this.server );
|
|
log ( 'monito server >>> ' + this.server );
|
|
this.type = this.settings.get_string ( "type" );
|
|
this.username = this.settings.get_string ( "username" );
|
|
this.name = this.settings.get_string ( "name" );
|
|
this.password = this.settings.get_string ( "password" );
|
|
this.urlcgi = this.settings.get_string ( "urlcgi" );
|
|
|
|
log ( 'monito name >>> ' + this.name );
|
|
log ( 'monito type >>> ' + this.type );
|
|
log ( 'monito username >>> ' + this.username );
|
|
log ( 'monito urlcgi >>> ' + this.urlcgi );
|
|
|
|
this.prepareHttp ( );
|
|
}
|
|
|
|
prepareHttp ( )
|
|
{
|
|
if (_httpSession === undefined) {
|
|
_httpSession = new Soup.Session();
|
|
_httpSession.user_agent = Me.metadata.uuid;
|
|
}
|
|
|
|
let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi, { 'format': 'json' } );
|
|
message.request_headers.append ( 'Accept', 'application/json' );
|
|
|
|
let auth = new Soup.AuthBasic()
|
|
auth.authenticate ( this.username, this.password );
|
|
message.request_headers.append ( "Authorization", auth.get_authorization ( message ) );
|
|
|
|
_httpSession.queue_message ( message, Lang.bind (this, this.handleMessage ) );
|
|
}
|
|
|
|
handleMessage ( _httpSession, message )
|
|
{
|
|
this.status = { };
|
|
this.status.service_status = [ ];
|
|
|
|
if ( message.status_code != Soup.Status.OK )
|
|
{
|
|
log ( '>>> error: ' + message.reason_phrase );
|
|
Main.notifyError ( 'Monito: ' + this.name,
|
|
'URL: ' + this.urlcgi + "\n" +
|
|
message.status_code + ': ' + message.reason_phrase );
|
|
this.error = message.reason_phrase;
|
|
}
|
|
else
|
|
{
|
|
let json = JSON.parse(message.response_body.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,
|
|
status_information: entry.service_output,
|
|
last_check: new Date ( parseInt(entry.service_last_state_change) * 1000 ) . toString(),
|
|
} );
|
|
}
|
|
this.error = null;
|
|
}
|
|
|
|
this.extension.refreshUI ( this );
|
|
return this.status != { };
|
|
}
|
|
}
|
|
|