monito/servers/icinga.js

106 lines
3.5 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 Icinga extends GenericServer {
constructor ( _server, extension ) {
super(_server, extension, 'Icinga');
}
refresh ( ) {
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 );
}
recheck ( entry )
{
// TODO: perhaps use a better idea, like if the urlcgi param
// is a directory, then appendinstead of changing... use a method for that?
let cmdcgi = this.urlcgi.replace ( /status.cgi/, 'cmd.cgi' );
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 ),
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();
message.button = entry.button;
this.authenticateAndSend ( message, this.handleCMDMessage );
}
handleMessage ( _httpSession, message )
{
let _data;
try {
_data = super.handleMessage ( _httpSession, message );
if ( this.error )
log ( 'Parent error ' + this.error );
else
{
let json = JSON.parse ( _data );
this.status = json.status;
}
this.extension.refreshUI ( this );
return ! this.error;
}
catch ( e )
{
log ( e );
log ( _data );
}
}
}