From c6c7c483199015f17865861e2f9b70d9b92992e5 Mon Sep 17 00:00:00 2001 From: Benjamin Drieu Date: Fri, 12 Nov 2021 12:11:20 +0100 Subject: [PATCH] Unfuck --- extension.js | 134 +++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 75 deletions(-) diff --git a/extension.js b/extension.js index 3cd5fd0..a1aaa02 100644 --- a/extension.js +++ b/extension.js @@ -38,11 +38,14 @@ const Main = imports.ui.main; const PanelMenu = imports.ui.panelMenu; const PopupMenu = imports.ui.popupMenu; -const { GObject, St, Soup, Clutter, Gio } = imports.gi; +const { GObject, St, Clutter, Gio } = imports.gi; const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito@drieu.org"; const Convenience = Me.imports.convenience; const Preferences = Me.imports.prefs; +const Icinga = Me.imports.servers.icinga.Icinga; +const Icinga2 = Me.imports.servers.icinga2.Icinga2; + let settings = Convenience.getSettings(SETTINGS_SCHEMA); @@ -147,22 +150,31 @@ class Indicator extends PanelMenu.Button { updateStatus ( ) { for ( let _server of Preferences.getServersList() ) { - let account_settings = Preferences.getAccountSettings ( _server ); + let _account_settings = Preferences.getAccountSettings ( _server ); - let username = account_settings.get_string("username"); - let password = account_settings.get_string("password"); - let urlcgi = account_settings.get_string("urlcgi"); + let type = _account_settings.get_string("type"); + 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 ( _server, urlcgi, { 'jsonoutput': '' } ); + else + { + let _serverLogic; + if ( type == 'Icinga' ) + _serverLogic = new Icinga ( _server ); + else if ( type == 'Icinga2' ) + _serverLogic = new Icinga ( _server ); + + if ( _serverLogic.refresh ( this ) ) + { + this.warningBoxes[_server].set_text ( '…' ); + this.criticalBoxes[_server].set_text ( '…' ); + } + } } } @@ -177,72 +189,44 @@ class Indicator extends PanelMenu.Button { return _bin; } - load_data_async ( server, url, params ) { - if (_httpSession === undefined) { - _httpSession = new Soup.Session(); - _httpSession.user_agent = Me.metadata.uuid; - } + refreshUI ( serverLogic ) { + this.initStatus ( ); // Specialize ! - let message = Soup.form_request_new_from_hash('GET', url, params); + this._box.remove_all_children(); + + for ( let i = 0 ; i < serverLogic.status.service_status.length ; i ++ ) + { + _status [ serverLogic.status.service_status[i].status ] ++; + if ( serverLogic.status.service_status[i].status != 'OK' ) + { + let infoBox = new St.BoxLayout({ + style_class: 'monito-service-line monito-service-line-' + serverLogic.status.service_status[i].status, + hover: true, + x_expand: true + }); + this._box.add_child(infoBox); - _httpSession.queue_message(message, Lang.bind(this, function(_httpSession, message) { -// Main.notify(message.response_body.data); - log ( '>>> ' + 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) ); - this.warningBoxes[server].set_text ( String(_status.WARNING) ); - this.criticalBoxes[server].set_text ( String(_status.CRITICAL) ); - } catch (e) { - Main.notify(_('Zbeu!')); - this.warningBoxes[server].set_text ( '…' ); - this.criticalBoxes[server].set_text ( '…' ); - log(e); - return; - } - })); + infoBox.add_child ( this.createBin ( serverLogic.status.service_status[i].status, + serverLogic.status.service_status[i].host_name, + 0 ) ); + infoBox.add_child ( this.createBin ( serverLogic.status.service_status[i].status, + serverLogic.status.service_status[i].service_display_name, + 1 ) ); + infoBox.add_child ( this.createBin ( serverLogic.status.service_status[i].status, + serverLogic.status.service_status[i].last_check, + 2) ); + infoBox.add_child ( this.createBin ( serverLogic.status.service_status[i].status, + serverLogic.status.service_status[i].attempts, + 3 ) ); + infoBox.add_child ( this.createBin ( serverLogic.status.service_status[i].status, + serverLogic.status.service_status[i].status_information, + 4 ) ); + } + } + + this.warningBoxes[serverLogic.server].set_text ( String(_status.WARNING) ); + this.criticalBoxes[serverLogic.server].set_text ( String(_status.CRITICAL) ); + return; }