Some refactoring

This commit is contained in:
Benjamin Drieu 2021-11-22 17:36:55 +01:00 committed by Benjamin Drieu
parent 886501cba2
commit 76319c11d5
4 changed files with 129 additions and 120 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 ( '…' );
@ -298,9 +298,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 +326,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' )
@ -347,8 +347,8 @@ class Indicator extends PanelMenu.Button {
}
}
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;
}

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 ) );

View File

@ -38,57 +38,34 @@ 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 );
urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
if (_httpSession === undefined) {
_httpSession = new Soup.Session();
_httpSession.user_agent = Me.metadata.uuid;
}
try {
let message = Soup.form_request_new_from_hash ( 'GET', urlcgi, { 'jsonoutput': '' } );
_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;
}
catch (e) {
Main.notify(_('Zbeu!'));
log(e);
return false;
}
this.buildURL ( );
this.prepareHttp ( );
}
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 );
}
handleMessage ( _httpSession, message )
{
let _data = super.handleMessage ( _httpSession, message );
let json = JSON.parse ( _data );
this.status = json.status;
this.error = null;
this.extension.refreshUI ( this );
return this.status != { };
}
}

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,72 +37,41 @@ 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 = [ ];
let _data = super.handleMessage ( _httpSession, message );
let json = JSON.parse ( _data );
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
if ( message.status_code != Soup.Status.OK )
for ( var entry of json )
{
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;
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(),
} );
}
else
{
let json = JSON.parse(message.response_body.data);
let _statuses = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ];
this.error = null;
for ( var entry of json )
{
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.error = null;
}
this.extension.refreshUI ( this );
this.extension.refreshUI ( this );
return this.status != { };
}
}