Refactor command sending

This commit is contained in:
Benjamin Drieu 2021-11-24 20:58:07 +01:00
parent 2eeb9f9655
commit 8812be23a6
2 changed files with 43 additions and 18 deletions

View File

@ -73,13 +73,13 @@ class GenericServer {
} }
authenticateAndSend ( message ) authenticateAndSend ( message, callback = this.handleMessage )
{ {
let auth = new Soup.AuthBasic() let auth = new Soup.AuthBasic()
auth.authenticate ( this.username, this.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 ) );
this._httpSession.queue_message ( message, Lang.bind (this, this.handleMessage ) ); this._httpSession.queue_message ( message, Lang.bind (this, callback ) );
} }

View File

@ -57,24 +57,28 @@ class Icinga extends GenericServer {
recheck ( entry ) recheck ( entry )
{ {
// TODO: perhaps use a better idea, like if the urlcgi param
// is a directory, then appendinstead of changing... use a method for that?
let cmdcgi = this.urlcgi.replace ( /status.cgi/, 'cmd.cgi' ); let cmdcgi = this.urlcgi.replace ( /status.cgi/, 'cmd.cgi' );
let params = { cmd_typ: '7', let d = new Date ( Date.now() );
cmd_mod: '2', var datestring = '%04d-%02d-%02d+%02d:%02d:%02d'.format ( d.getFullYear(), d.getMonth() + 1, d.getDate(),
hostservice: '%s^%s'.format ( entry.host_name, entry.service_description.replaceAll ( ' ', '+' ) ), d.getHours(), d.getMinutes(), d.getSeconds() );
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' ); // We have to do this manually since the default encoding of
this.authenticateAndSend ( message ); // Soup.form_request_new_from_hash is not understood by Icinga ... a shame!
// cmd_typ: 7 let params = 'cmd_typ=7&cmd_mod=2&host=%s&service=%s&start_time=%s&force_check=on&com_data=Recheck+by+Monito&btnSubmit=Commit' .
// cmd_mod: 2 format ( encodeURI ( entry.host_name ),
// start_time: '2021-11-22 17:21:12' encodeURI ( entry.service_description ),
// hostservice: srv03^servicename (avec encodage + à la place des ' ') encodeURI ( datestring ) );
// force_check: on
let message = Soup.form_request_new_from_hash ( 'POST', cmdcgi, { } );
message.request_body.truncate();
message.request_body.append ( params );
message.request_body.flatten();
// TODO: change button to spinner
this.authenticateAndSend ( message, this.handleCMDMessage );
} }
handleMessage ( _httpSession, message ) handleMessage ( _httpSession, message )
@ -101,4 +105,25 @@ class Icinga extends GenericServer {
} }
} }
handleCMDMessage ( _httpSession, message )
{
let _data;
try {
_data = super.handleMessage ( _httpSession, message );
if ( this.error )
log ( 'Parent error ' + this.error );
else
{
// if error, grep for class='errorMessage'
// else grep for class='successBox'
log ( 'Cmd output ' + _data );
}
}
catch ( e )
{
log ( e );
log ( _data );
}
}
} }