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
1 changed files with 60 additions and 58 deletions

View File

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