Some refactoring

This commit is contained in:
Benjamin Drieu 2021-11-18 01:32:48 +01:00
parent 9317c4cb7a
commit 2a36fc113c

View File

@ -38,74 +38,76 @@ 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' } );
message.request_headers.append ( 'Accept', 'application/json' );
let auth = new Soup.AuthBasic()
auth.authenticate ( username, password );
message.request_headers.append ( "Authorization", auth.get_authorization ( message ) );
_httpSession.queue_message(message, Lang.bind
(this, function(_httpSession, message)
{
// TODO format this in the manner of icinga1
// log ( '>>> ' + message.response_body.data );
let json = JSON.parse(message.response_body.data);
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
this.status = { };
this.status.service_status = [ ];
for ( var entry of json )
{
// if ( entry.status != 0 )
// log ( JSON.stringify(entry, null, 2) );
this.status.service_status.push ( {
status: _statuses [ entry.service_state ],
host_name: entry.host_name,
service_display_name: entry.service_display_name,
attempts: entry.service_attempt,
status_information: entry.service_output,
last_check: new Date ( parseInt(entry.service_last_state_change) * 1000 ) . toString(),
} );
}
extension.refreshUI ( this );
}
)
);
return true;
}
catch (e) {
Main.notify(_('Zbeu!'));
log(e);
return false;
}
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 ) );
}
handleMessage ( _httpSession, message )
{
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' ];
this.status = { };
this.status.service_status = [ ];
for ( var entry of json )
{
// if ( entry.status != 0 )
// log ( JSON.stringify(entry, null, 2) );
this.status.service_status.push ( {
status: _statuses [ entry.service_state ],
host_name: entry.host_name,
service_display_name: entry.service_display_name,
attempts: entry.service_attempt,
status_information: entry.service_output,
last_check: new Date ( parseInt(entry.service_last_state_change) * 1000 ) . toString(),
} );
}
this.extension.refreshUI ( this );
return true;
}
}