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()
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 ) );
this._httpSession.queue_message ( message, Lang.bind (this, callback ) );
}

View File

@ -57,24 +57,28 @@ class Icinga extends GenericServer {
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 params = { cmd_typ: '7',
cmd_mod: '2',
hostservice: '%s^%s'.format ( entry.host_name, entry.service_description.replaceAll ( ' ', '+' ) ),
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 ) );
let d = new Date ( Date.now() );
var datestring = '%04d-%02d-%02d+%02d:%02d:%02d'.format ( d.getFullYear(), d.getMonth() + 1, d.getDate(),
d.getHours(), d.getMinutes(), d.getSeconds() );
// message.request_headers.append ( 'Accept', 'application/json' );
this.authenticateAndSend ( message );
// cmd_typ: 7
// cmd_mod: 2
// start_time: '2021-11-22 17:21:12'
// hostservice: srv03^servicename (avec encodage + à la place des ' ')
// force_check: on
// We have to do this manually since the default encoding of
// Soup.form_request_new_from_hash is not understood by Icinga ... a shame!
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' .
format ( encodeURI ( entry.host_name ),
encodeURI ( entry.service_description ),
encodeURI ( datestring ) );
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 )
@ -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 );
}
}
}