monito/servers/icinga.js

105 lines
3.2 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 ) {
super(_server, 'Icinga');
}
refresh ( extension ) {
this.extension = extension;
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 )
{
let cmdcgi = this.urlcgi.replace ( /status.cgi/, 'cmd.cgi' );
let params = { cmd_typ: '7',
cmd_mod: '2',
hostservice: '%s^%s'.format ( entry.host_name, entry.service_description.replaceAll ( ' ', '+' ) ),
start_time: '2021-11-22%2018:13:48',
force_check: 'on',
btnSubmit: 'Commit',
};
let message = Soup.form_request_new_from_hash ( 'POST', cmdcgi, params );
log ( JSON.stringify ( params ) );
// message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message );
// cmd_typ: 7
// cmd_mod: 2
// start_time: '2021-11-22 17:21:12'
// hostservice: srv03^servicename (avec encodage + à la place des ' ')
// force_check: on
}
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.status != { };
}
catch ( e )
{
log ( e );
log ( _data );
}
}
}