Some refactoring

This commit is contained in:
Benjamin Drieu 2021-11-18 01:32:48 +01:00 committed by Benjamin Drieu
parent 31b39b8eb7
commit 33522a08f9

View File

@ -38,41 +38,53 @@ class Icinga2 {
}
refresh ( extension ) {
let _settings = Preferences.getAccountSettings ( this.server );
this.extension = extension;
this.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.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 >>> ' + name );
log ( 'monito type >>> ' + type );
log ( 'monito username >>> ' + username );
log ( 'monito urlcgi >>> ' + urlcgi );
log ( 'monito name >>> ' + this.name );
log ( 'monito type >>> ' + this.type );
log ( 'monito username >>> ' + this.username );
log ( 'monito urlcgi >>> ' + this.urlcgi );
// urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
this.prepareHttp ( );
}
prepareHttp ( )
{
if (_httpSession === undefined) {
_httpSession = new Soup.Session();
_httpSession.user_agent = Me.metadata.uuid;
}
try {
let message = Soup.form_request_new_from_hash ( 'GET', urlcgi, { 'format': 'json' } );
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 ( username, password );
auth.authenticate ( this.username, this.password );
message.request_headers.append ( "Authorization", auth.get_authorization ( message ) );
_httpSession.queue_message ( message, Lang.bind (this, this.handleMessage ) );
}
_httpSession.queue_message(message, Lang.bind
(this, function(_httpSession, message)
handleMessage ( _httpSession, message )
{
// TODO format this in the manner of icinga1
// log ( '>>> ' + message.response_body.data );
log ( '>>> ' + message.status_code );
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 );
return;
}
let json = JSON.parse(message.response_body.data);
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
@ -81,8 +93,8 @@ class Icinga2 {
for ( var entry of json )
{
// if ( entry.status != 0 )
// log ( JSON.stringify(entry, null, 2) );
// if ( entry.status != 0 )
// log ( JSON.stringify(entry, null, 2) );
this.status.service_status.push ( {
status: _statuses [ entry.service_state ],
@ -94,18 +106,8 @@ class Icinga2 {
} );
}
extension.refreshUI ( this );
}
)
);
this.extension.refreshUI ( this );
return true;
}
catch (e) {
Main.notify(_('Zbeu!'));
log(e);
return false;
}
}
}