monito/servers/icinga.js

110 lines
3.7 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
let _httpSession;
class Icinga extends GenericServer {
2021-12-03 16:35:50 +01:00
constructor ( _server, extension ) {
super(_server, extension, 'Icinga');
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 ( );
this.prepareHttp ( );
}
prepareHttp ( )
{
super.prepareHttp ( );
let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi, { 'jsonoutput': '' } );
message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message );
2021-11-12 18:26:31 +01:00
}
2021-11-22 17:36:55 +01:00
2021-11-22 22:12:49 +01:00
recheck ( entry )
{
2021-11-24 20:58:07 +01:00
// TODO: perhaps use a better idea, like if the urlcgi param
// is a directory, then appendinstead of changing... use a method for that?
2021-11-22 22:12:49 +01:00
let cmdcgi = this.urlcgi.replace ( /status.cgi/, 'cmd.cgi' );
2021-11-24 20:58:07 +01:00
let d = new Date ( Date.now() );
var datestring = '%04d-%02d-%02d+%02d:%02d:%02d'.format ( d.getFullYear(), d.getMonth() + 1, d.getDate(),
d.getHours(), d.getMinutes(), d.getSeconds() );
// We have to do this manually since the default encoding of
// Soup.form_request_new_from_hash is not understood by Icinga ... a shame!
let params = 'cmd_typ=7&cmd_mod=2&host=%s&service=%s&start_time=%s&force_check=on&com_data=Recheck+by+Monito&btnSubmit=Commit' .
format ( encodeURI ( entry.real_host_name ),
encodeURI ( entry.real_service_display_name ),
2021-11-24 20:58:07 +01:00
encodeURI ( datestring ) );
let message = Soup.form_request_new_from_hash ( 'POST', cmdcgi, { } );
message.request_body.truncate();
message.request_body.append ( params );
message.request_body.flatten();
2021-12-03 16:35:50 +01:00
message.button = entry.button;
2021-11-24 20:58:07 +01:00
this.authenticateAndSend ( message, this.handleCMDMessage );
2021-11-22 22:12:49 +01:00
}
2021-11-22 17:36:55 +01:00
handleMessage ( _httpSession, message )
{
2021-11-22 22:12:49 +01:00
let _data;
try {
_data = super.handleMessage ( _httpSession, message );
if ( this.error )
log ( 'Parent error ' + this.error );
else
{
let json = JSON.parse ( _data );
2021-12-06 11:34:55 +01:00
2021-11-22 22:12:49 +01:00
this.status = json.status;
2021-12-06 11:34:55 +01:00
log ( this.status );
for ( var entry of this.status.service_status )
entry.last_check = this.formatDate ( entry.last_check );
2021-11-22 22:12:49 +01:00
}
this.extension.refreshUI ( this );
2021-11-25 14:45:42 +01:00
return ! this.error;
2021-11-22 22:12:49 +01:00
}
catch ( e )
{
log ( e );
log ( _data );
}
2021-11-22 17:36:55 +01:00
}
2021-11-24 20:58:07 +01:00
2021-11-12 18:26:31 +01:00
}