295 lines
9.0 KiB
JavaScript
295 lines
9.0 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
|
|
*/
|
|
|
|
/* exported init */
|
|
|
|
const GETTEXT_DOMAIN = 'monito';
|
|
|
|
let _httpSession;
|
|
let _status;
|
|
let _ok_text;
|
|
let _warning_text;
|
|
let _critical_text;
|
|
|
|
const Gettext = imports.gettext.domain(GETTEXT_DOMAIN);
|
|
const _ = Gettext.gettext;
|
|
const Lang = imports.lang;
|
|
|
|
const ExtensionUtils = imports.misc.extensionUtils;
|
|
const Me = ExtensionUtils.getCurrentExtension();
|
|
const Main = imports.ui.main;
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
const { GObject, St, Soup, Clutter } = imports.gi;
|
|
|
|
const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito@drieu.org";
|
|
const Convenience = Me.imports.convenience;
|
|
let settings = Convenience.getSettings(SETTINGS_SCHEMA);
|
|
|
|
|
|
const Indicator = GObject.registerClass(
|
|
class Indicator extends PanelMenu.Button {
|
|
_init() {
|
|
super._init(0.0, _('Monito Checker'));
|
|
|
|
let box = new St.BoxLayout ( { } );
|
|
this.add_child(box);
|
|
|
|
this.initStatus ( );
|
|
|
|
// Main Box
|
|
/*
|
|
let ok_box = new St.BoxLayout({ style_class: 'monito-ok-box monito-box' });
|
|
_ok_text = new St.Label({ text: String(_status['OK']) })
|
|
ok_box.add_child(_ok_text);
|
|
box.add_child(ok_box);
|
|
*/
|
|
let warning_box = new St.BoxLayout({ style_class: 'monito-warning-box monito-box' });
|
|
_warning_text = new St.Label({ text: String(_status['WARNING']) })
|
|
warning_box.add_child(_warning_text);
|
|
box.add_child(warning_box);
|
|
|
|
let critical_box = new St.BoxLayout({ style_class: 'monito-critical-box monito-box' });
|
|
_critical_text = new St.Label({ text: String(_status['CRITICAL']) })
|
|
critical_box.add_child(_critical_text);
|
|
box.add_child(critical_box);
|
|
|
|
box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM));
|
|
|
|
|
|
// Menu
|
|
this._buttonMenu = new PopupMenu.PopupBaseMenuItem({
|
|
reactive: false,
|
|
style_class: 'monito-menu-button-container',
|
|
});
|
|
this.menu.addMenuItem(this._buttonMenu);
|
|
|
|
|
|
// let item = new PopupMenu.PopupMenuItem(_('Reload'));
|
|
// item.connect('activate', () => {
|
|
// this.updateStatus ( );
|
|
// });
|
|
// this.menu.addMenuItem(item);
|
|
|
|
this._mainLabel = new St.Label({ style_class: 'monito-title', text: 'Monito Checker', x_expand: true });
|
|
this._buttonMenu.actor.add_actor(this._mainLabel);
|
|
|
|
|
|
this._prefsButton = this._createButton ( 'preferences-system-symbolic', 'Preferences', this._onPreferencesActivate );
|
|
this._buttonMenu.actor.add_child (this._prefsButton);
|
|
|
|
this._reloadButton = this._createButton ( 'view-refresh-symbolic', 'Reload', this.updateStatus );
|
|
this._buttonMenu.actor.add_child (this._reloadButton );
|
|
|
|
let _intermediate = new PopupMenu.PopupBaseMenuItem ( {
|
|
style_class: 'monito-services',
|
|
reactive: false
|
|
});
|
|
|
|
this.menu.addMenuItem(_intermediate);
|
|
this._box = new St.BoxLayout({
|
|
style_class: 'monito-services',
|
|
vertical: true,
|
|
x_expand: true
|
|
});
|
|
_intermediate.actor.add_actor(this._box);
|
|
|
|
this.updateStatus ( );
|
|
}
|
|
|
|
initStatus ( ) {
|
|
_status = { 'OK': 0,
|
|
'WARNING': 0,
|
|
'CRITICAL': 0,
|
|
'UNKNOWN': 0 };
|
|
}
|
|
|
|
updateStatus ( ) {
|
|
const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito@drieu.org.account";
|
|
const Convenience = Me.imports.convenience;
|
|
let account_settings = Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, '/org/gnome/shell/extensions/monito@drieu.org/account/0/');
|
|
|
|
let username = account_settings.get_string("username");
|
|
let password = account_settings.get_string("password");
|
|
let urlcgi = account_settings.get_string("urlcgi");
|
|
|
|
if ( ! urlcgi )
|
|
{
|
|
log ( 'Not updating monito because no URL configured' );
|
|
return;
|
|
}
|
|
|
|
urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
|
|
// log ( 'monito >>> ' + urlcgi );
|
|
|
|
this.load_data_async ( urlcgi, { 'jsonoutput': '' }, function(res) { Main.notify(res); } )
|
|
}
|
|
|
|
createBin(status, text, col) {
|
|
let _widths = [ 300, 300, 200, 50, 600 ];
|
|
let _bin = new St.Bin({
|
|
style_class: 'monito-service-' + status,
|
|
width: _widths[col],
|
|
x_expand: ( col == 4 ? true : false ),
|
|
child: new St.Label({ style_class: 'monito-label', text: text })
|
|
});
|
|
return _bin;
|
|
}
|
|
|
|
load_data_async(url, params, fun) {
|
|
if (_httpSession === undefined) {
|
|
_httpSession = new Soup.Session();
|
|
_httpSession.user_agent = Me.metadata.uuid;
|
|
} else {
|
|
// abort previous requests.
|
|
_httpSession.abort();
|
|
}
|
|
|
|
let message = Soup.form_request_new_from_hash('GET', url, params);
|
|
|
|
_httpSession.queue_message(message, Lang.bind(this, function(_httpSession, message) {
|
|
//Main.notify(message.response_body.data);
|
|
try {
|
|
// log ( message.response_body.data );
|
|
let json = JSON.parse(message.response_body.data);
|
|
|
|
this.initStatus ( );
|
|
|
|
this._box.remove_all_children();
|
|
|
|
for ( let i = 0 ; i < json.status.service_status.length ; i ++ )
|
|
{
|
|
_status [ json.status.service_status[i].status ] ++;
|
|
if ( json.status.service_status[i].status != 'OK' )
|
|
{
|
|
let infoBox = new St.BoxLayout({
|
|
style_class: 'monito-service-line monito-service-line-' + json.status.service_status[i].status,
|
|
hover: true,
|
|
x_expand: true
|
|
});
|
|
this._box.add_child(infoBox);
|
|
|
|
infoBox.add_child ( this.createBin ( json.status.service_status[i].status,
|
|
json.status.service_status[i].host_name,
|
|
0 ) );
|
|
infoBox.add_child ( this.createBin ( json.status.service_status[i].status,
|
|
json.status.service_status[i].service_display_name,
|
|
1 ) );
|
|
infoBox.add_child ( this.createBin ( json.status.service_status[i].status,
|
|
json.status.service_status[i].last_check,
|
|
2) );
|
|
infoBox.add_child ( this.createBin ( json.status.service_status[i].status,
|
|
json.status.service_status[i].attempts,
|
|
3 ) );
|
|
infoBox.add_child ( this.createBin ( json.status.service_status[i].status,
|
|
json.status.service_status[i].status_information,
|
|
4 ) );
|
|
|
|
// let url = 'https://xxx:xxx@host/cgi-bin/icinga/extinfo.cgi?type=2&host='+json.status.service_status[i].host_name+"&service="+json.status.service_status[i].service_description;
|
|
// activeLabel.connect('button-press-event', () => {
|
|
// Main.notify(url);
|
|
// Gtk.show_uri(null, url, global.get_current_time());
|
|
// } );
|
|
}
|
|
}
|
|
|
|
// _ok_text.set_text ( String(_status.OK) );
|
|
_warning_text.set_text ( String(_status.WARNING) );
|
|
_critical_text.set_text ( String(_status.CRITICAL) );
|
|
} catch (e) {
|
|
Main.notify(_('Zbeu!'));
|
|
_warning_text.set_text ( '…' );
|
|
_critical_text.set_text ( '…' );
|
|
log(e);
|
|
return;
|
|
}
|
|
}));
|
|
return;
|
|
}
|
|
|
|
_onPreferencesActivate() {
|
|
this.menu.actor.hide();
|
|
if (typeof ExtensionUtils.openPrefs === 'function') {
|
|
ExtensionUtils.openPrefs();
|
|
} else {
|
|
Util.spawn([
|
|
"gnome-shell-extension-prefs",
|
|
Me.uuid
|
|
]);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
_createButton ( icon, text, callback ) {
|
|
let button = new St.Button({
|
|
x_align: Clutter.ActorAlign.END,
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
reactive: true,
|
|
can_focus: true,
|
|
track_hover: true,
|
|
accessible_name: text,
|
|
style_class: 'button'
|
|
});
|
|
|
|
button.child = new St.Icon({
|
|
style_class: 'monito-button-icon',
|
|
icon_name: icon,
|
|
icon_size: 24,
|
|
width: 24,
|
|
height: 24,
|
|
});
|
|
|
|
button.connect('clicked', Lang.bind(this, callback ) );
|
|
|
|
return button;
|
|
}
|
|
|
|
});
|
|
|
|
class Extension {
|
|
constructor(uuid) {
|
|
this._uuid = uuid;
|
|
|
|
ExtensionUtils.initTranslations(GETTEXT_DOMAIN);
|
|
}
|
|
|
|
enable() {
|
|
this._indicator = new Indicator();
|
|
Main.panel.addToStatusArea(this._uuid, this._indicator);
|
|
}
|
|
|
|
disable() {
|
|
this._indicator.destroy();
|
|
this._indicator = null;
|
|
|
|
if (_httpSession !== undefined)
|
|
_httpSession.abort();
|
|
|
|
_httpSession = undefined;
|
|
}
|
|
}
|
|
|
|
function init(meta) {
|
|
return new Extension(meta.uuid);
|
|
}
|