Compare commits

...

2 Commits

Author SHA1 Message Date
Benjamin Drieu d533f08e44 Add more loging + error control 2021-11-22 22:12:49 +01:00
Benjamin Drieu 76319c11d5 Some refactoring 2021-11-22 17:36:55 +01:00
4 changed files with 153 additions and 97 deletions

View File

@ -85,9 +85,9 @@ class Indicator extends PanelMenu.Button {
let type = this.account_settings.get_string ( "type" );
if ( type == 'Icinga' )
this.serverlogic = new Icinga ( this.server );
this.serverLogic = new Icinga ( this.server );
else if ( type == 'Icinga2' )
this.serverlogic = new Icinga2 ( this.server );
this.serverLogic = new Icinga2 ( this.server );
this.initUI ( );
}
@ -197,7 +197,7 @@ class Indicator extends PanelMenu.Button {
updateStatus ( )
{
if ( ! this.serverlogic.refresh ( this ) )
if ( ! this.serverLogic.refresh ( this ) )
{
this.warningBoxes[this.server].set_text ( '…' );
this.criticalBoxes[this.server].set_text ( '…' );
@ -274,6 +274,8 @@ class Indicator extends PanelMenu.Button {
y_align: Clutter.ActorAlign.CENTER,
can_focus: true,
} );
_button.service = text;
_button.connect ( 'clicked', Lang.bind ( this, this._onRecheckButtonClick ) );
_button.child = new St.Icon ( {
icon_name: 'view-refresh-symbolic',
icon_size: 16,
@ -298,9 +300,9 @@ class Indicator extends PanelMenu.Button {
this._box.remove_all_children();
if ( this.serverlogic.error )
if ( this.serverLogic.error )
{
this._box.add_child ( new St.Label ( { style_class: 'monito-network-error', text: this.serverlogic.error } ) );
this._box.add_child ( new St.Label ( { style_class: 'monito-network-error', text: this.serverLogic.error } ) );
return;
}
@ -326,7 +328,7 @@ class Indicator extends PanelMenu.Button {
});
scrollBox.add_actor(tableBox);
for ( let entry of this.serverlogic.getProcessedStatus ( ) )
for ( let entry of this.serverLogic.getProcessedStatus ( ) )
{
_status [ entry.status ] ++;
if ( entry.status != 'OK' )
@ -343,12 +345,12 @@ class Indicator extends PanelMenu.Button {
{
infoBox.add_child ( this.createBin ( entry.status, entry [ _col ], column_definitions [ _col ] ) );
}
infoBox.add_child ( this.createBin ( entry.status, '', column_definitions [ 'actions' ] ) );
infoBox.add_child ( this.createBin ( entry.status, entry, column_definitions [ 'actions' ] ) );
}
}
this.warningBoxes[this.serverlogic.server].set_text ( String(_status.WARNING) );
this.criticalBoxes[this.serverlogic.server].set_text ( String(_status.CRITICAL) );
this.warningBoxes[this.server].set_text ( String(_status.WARNING) );
this.criticalBoxes[this.server].set_text ( String(_status.CRITICAL) );
return;
}
@ -386,6 +388,12 @@ class Indicator extends PanelMenu.Button {
this.refreshUI ( );
}
_onRecheckButtonClick ( e )
{
monitoLog ( JSON.stringify ( e.service ) );
this.serverLogic.recheck ( e.service );
}
_createButton ( icon, text, callback ) {
let button = new St.Button({
x_align: Clutter.ActorAlign.END,

View File

@ -28,23 +28,88 @@ const Main = imports.ui.main;
const Me = ExtensionUtils.getCurrentExtension();
const Preferences = Me.imports.prefs;
let _httpSession;
class GenericServer {
constructor ( _server, _serverType = 'Generic' )
{
log ( '>>> New %s server #%s'.format ( _serverType, _server ) );
this.server = _server;
this._server = _server;
this._settings = Preferences.getAccountSettings ( this._server );
this._httpSession = null;
this._url = null;
}
getServer ( )
{
return this._server;
}
buildURL ( )
{
if ( ! this._settings )
this._settings = Preferences.getAccountSettings ( this._server );
this.type = this._settings.get_string ( "type" );
this.username = this._settings.get_string ( "username" );
this.name = this._settings.get_string ( "name" );
this.password = this._settings.get_string ( "password" );
this.urlcgi = this._settings.get_string ( "urlcgi" );
log ( 'monito server >>> ' + this._server );
log ( 'monito name >>> ' + this.name );
log ( 'monito type >>> ' + this.type );
log ( 'monito username >>> ' + this.username );
log ( 'monito urlcgi >>> ' + this.urlcgi );
}
prepareHttp ( )
{
if ( this._httpSession == null ) {
this._httpSession = new Soup.Session();
this._httpSession.user_agent = Me.metadata.uuid;
}
}
authenticateAndSend ( message )
{
let auth = new Soup.AuthBasic()
auth.authenticate ( this.username, this.password );
message.request_headers.append ( "Authorization", auth.get_authorization ( message ) );
this._httpSession.queue_message ( message, Lang.bind (this, this.handleMessage ) );
}
handleMessage ( _httpSession, message )
{
this.status = { };
this.status.service_status = [ ];
if ( message.status_code != Soup.Status.OK )
{
log ( '>>> error: ' + message.reason_phrase );
Main.notifyError ( 'Monito: ' + this.name,
'URL: ' + this.urlcgi + "\n" +
message.status_code + ': ' + message.reason_phrase );
this.error = message.reason_phrase;
return null;
}
else
{
return message.response_body.data;
}
}
getProcessedStatus ( )
{
let status = this.status.service_status;
this.columns = Preferences.getColumns(this.server);
this.sortOrder = Preferences.getSortOrder(this.server);
this.columns = Preferences.getColumns(this._server);
this.sortOrder = Preferences.getSortOrder(this._server);
log ( this.sortOrder );
status = status.sort ( Lang.bind ( this, this.compareServices ) );
@ -52,7 +117,7 @@ class GenericServer {
return status;
}
compareServices ( a, b )
{
for ( let _comparison of this.sortOrder )

View File

@ -38,57 +38,68 @@ class Icinga extends GenericServer {
}
refresh ( extension ) {
let _settings = Preferences.getAccountSettings ( this.server );
log ( 'monito server >>> ' + this.server );
let type = _settings.get_string ( "type" );
let username = _settings.get_string ( "username" );
let name = _settings.get_string ( "name" );
let password = _settings.get_string ( "password" );
let urlcgi = _settings.get_string ( "urlcgi" );
this.extension = extension;
log ( 'monito name >>> ' + name );
log ( 'monito type >>> ' + type );
log ( 'monito username >>> ' + username );
log ( 'monito urlcgi >>> ' + urlcgi );
this.buildURL ( );
this.prepareHttp ( );
}
urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
if (_httpSession === undefined) {
_httpSession = new Soup.Session();
_httpSession.user_agent = Me.metadata.uuid;
}
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.error = null;
}
let message = Soup.form_request_new_from_hash ( 'GET', urlcgi, { 'jsonoutput': '' } );
this.extension.refreshUI ( this );
_httpSession.queue_message(message, Lang.bind
(this, function(_httpSession, message)
{
if ( message.status_code != Soup.Status.OK )
{
log ( '>>> error: ' + message.reason_phrase );
Main.notify ( 'Monito: ' + name, message.reason_phrase );
this.status = null;
this.error = message.reason_phrase;
}
else
{
let json = JSON.parse(message.response_body.data);
this.status = json.status;
this.error = null;
}
extension.refreshUI ( this );
}
)
);
return this.status !== null;
return this.status != { };
}
catch (e) {
Main.notify(_('Zbeu!'));
log(e);
return false;
catch ( e )
{
log ( e );
log ( _data );
}
}
}

View File

@ -29,8 +29,6 @@ const Me = ExtensionUtils.getCurrentExtension();
const Preferences = Me.imports.prefs;
const GenericServer = Me.imports.servers.genericserver.GenericServer;
let _httpSession;
class Icinga2 extends GenericServer {
constructor ( _server ) {
@ -39,55 +37,29 @@ class Icinga2 extends GenericServer {
refresh ( extension ) {
this.extension = extension;
this.settings = Preferences.getAccountSettings ( this.server );
log ( 'monito server >>> ' + this.server );
this.type = this.settings.get_string ( "type" );
this.username = this.settings.get_string ( "username" );
this.name = this.settings.get_string ( "name" );
this.password = this.settings.get_string ( "password" );
this.urlcgi = this.settings.get_string ( "urlcgi" );
log ( 'monito name >>> ' + this.name );
log ( 'monito type >>> ' + this.type );
log ( 'monito username >>> ' + this.username );
log ( 'monito urlcgi >>> ' + this.urlcgi );
this.buildURL ( );
this.prepareHttp ( );
}
prepareHttp ( )
{
if (_httpSession === undefined) {
_httpSession = new Soup.Session();
_httpSession.user_agent = Me.metadata.uuid;
}
super.prepareHttp ( );
let message = Soup.form_request_new_from_hash ( 'GET', this.urlcgi, { 'format': 'json' } );
message.request_headers.append ( 'Accept', 'application/json' );
let auth = new Soup.AuthBasic()
auth.authenticate ( this.username, this.password );
message.request_headers.append ( "Authorization", auth.get_authorization ( message ) );
_httpSession.queue_message ( message, Lang.bind (this, this.handleMessage ) );
this.authenticateAndSend ( message );
}
handleMessage ( _httpSession, message )
{
this.status = { };
this.status.service_status = [ ];
if ( message.status_code != Soup.Status.OK )
{
log ( '>>> error: ' + message.reason_phrase );
Main.notifyError ( 'Monito: ' + this.name,
'URL: ' + this.urlcgi + "\n" +
message.status_code + ': ' + message.reason_phrase );
this.error = message.reason_phrase;
}
let _data = super.handleMessage ( _httpSession, message );
if ( this.error )
log ( 'Parent error ' + this.error );
else
{
let json = JSON.parse(message.response_body.data);
let json = JSON.parse ( _data );
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
for ( var entry of json )
@ -104,7 +76,7 @@ class Icinga2 extends GenericServer {
this.error = null;
}
this.extension.refreshUI ( this );
this.extension.refreshUI ( this );
return this.status != { };
}
}