monito/servers/icinga2api.js

138 lines
5.4 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;
var Icinga2API = class extends GenericServer {
constructor ( _server, extension ) {
super(_server, extension, 'Icinga2 API');
}
refresh ( ) {
this.buildURL ( );
this.prepareHttp ( );
}
prepareHttp ( )
{
super.prepareHttp ( );
let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi + '/objects/services', { } );
message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message, this.handlePollMessage );
}
recheck ( entry )
{
let message = Soup.form_request_new_from_hash ( 'POST', this.urlcgi + '/actions/reschedule-check', { } );
let params = '{ "type": "Service", "filter": "host.name==\\"%s\\" && service.name==\\"%s\\"", "force": true, "pretty": true }' . format ( entry.real_host_name, entry.real_service_display_name );
message.request_body.truncate();
message.request_body.append ( params );
message.request_body.flatten();
message.button = entry.button;
message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message, this.handleCMDMessage );
}
recheckAll ( button )
{
let message = Soup.form_request_new_from_hash ( 'POST', this.urlcgi + '/actions/reschedule-check', { } );
let params = '{ "type": "Service", "force": true, "pretty": true }';
message.request_body.truncate();
message.request_body.append ( params );
message.request_body.flatten();
message.button = button;
message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message, this.handleCMDMessage );
}
handlePollMessage ( _httpSession, message )
{
this.monitoLog ( 'handlePollMessage' );
let _data = super.handleMessage ( _httpSession, message );
try
{
if ( this.error )
this.monitoLog ( 'Parent error ' + this.error );
else
{
let json = JSON.parse ( _data );
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN', 'PENDING' ];
for ( var entry of json.results )
{
this.monitoLog ( JSON.stringify(entry) );
var _attrs = entry.attrs;
_attrs [ 'real_host_name' ] = entry.attrs.host_name.repeat ( 1 );
_attrs [ 'status' ] = _statuses [ entry.attrs.state ];
_attrs [ 'service_display_name' ] = entry.attrs.display_name.repeat ( 1 );
_attrs [ 'has_been_acknowledged' ] = parseInt ( entry.attrs.acknowledgement );
_attrs [ 'attempts' ] = '%d/%d'.format ( entry.attrs.check_attempt, entry.attrs.max_check_attempts );
_attrs [ 'status_information' ] = ( entry.attrs.last_check_result ? entry.attrs.last_check_result.output : _statuses[entry.attrs.state] );
_attrs [ 'real_last_state_change' ] = entry.attrs.last_state_change;
_attrs [ 'last_state_change' ] = ( entry.attrs.last_state_change ? this.formatDate(parseInt(entry.attrs.last_state_change)) : '' );
_attrs [ 'real_last_check' ] = entry.attrs.last_check;
_attrs [ 'last_check' ] = ( entry.attrs.last_check ? this.formatDate(parseInt(entry.attrs.last_check)) : '' );
_attrs [ 'real_next_check' ] = entry.attrs.next_check;
_attrs [ 'next_check' ] = ( entry.attrs.next_check ? this.formatDate(parseInt(entry.attrs.next_check)) : '' );
this.status.service_status.push ( _attrs );
}
}
}
catch ( e )
{
this.monitoLog ( '> ERROR: ' + e );
this.monitoLog ( '> DATA: ' + _data );
}
this.extension.refreshUI ( this );
return ! this.error;
}
getUrlForService ( service )
{
return '%s/monitoring/service/show?host=%s&service=%s'.format ( this._settings.get_string ( 'url' ),
encodeURI ( service.real_host_name ),
encodeURI ( service.service_display_name ) );
}
}