Added trigger statistics to Prometheus-style outputs, fixed executable triggers not correctly responding with the default on execv fail

This commit is contained in:
Thulinma 2018-12-13 14:56:52 +01:00
parent e18f2f0b47
commit 8b7757c8e9
5 changed files with 89 additions and 4 deletions

View file

@ -366,6 +366,19 @@ pid_t Util::Procs::StartPiped(const char * const * argv, int * fdin, int * fdout
}
//Because execvp requires a char* const* and we have a const char* const*
execvp(argv[0], (char* const*)argv);
/*LTS-START*/
char * trggr = getenv("MIST_TRIGGER");
if (trggr && strlen(trggr)){
ERROR_MSG("%s trigger failed to execute %s: %s", trggr, argv[0], strerror(errno));
JSON::Value j;
j["trigger_fail"] = trggr;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(j.toString());
std::cout << getenv("MIST_TRIG_DEF");
exit(42);
}
/*LTS-END*/
ERROR_MSG("execvp failed for process %s, reason: %s", argv[0], strerror(errno));
exit(42);
} else if (pid == -1) {

View file

@ -20,10 +20,22 @@
#include "procs.h" //for StartPiped
#include "shared_memory.h"
#include "util.h"
#include "timing.h"
#include <string.h> //for strncmp
namespace Triggers{
static void submitTriggerStat(const std::string trigger, uint64_t millis, bool ok){
JSON::Value j;
j["trigger_stat"]["name"] = trigger;
j["trigger_stat"]["ms"] = Util::bootMS()-millis;
j["trigger_stat"]["ok"] = ok;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(j.toString());
}
///\brief Handles a trigger by sending a payload to a destination.
///\param trigger Trigger event type.
///\param value Destination. This can be an (HTTP)URL, or an absolute path to a binary/script
@ -31,6 +43,7 @@ namespace Triggers{
///\param sync If true, handler is executed blocking and uses the response data.
///\returns String, false if further processing should be aborted.
std::string handleTrigger(const std::string &trigger, const std::string &value, const std::string &payload, int sync, const std::string &defaultResponse){
uint64_t tStartMs = Util::bootMS();
if (!value.size()){
WARN_MSG("Trigger requested with empty destination");
return "true";
@ -42,21 +55,29 @@ namespace Triggers{
DL.setHeader("Content-Type", "text/plain");
HTTP::URL url(value);
if (DL.post(url, payload, sync) && sync && DL.isOk()){
submitTriggerStat(trigger, tStartMs, true);
return DL.data();
}
FAIL_MSG("Trigger failed to execute (%s), using default response: %s", DL.getStatusText().c_str(), defaultResponse.c_str());
submitTriggerStat(trigger, tStartMs, false);
return defaultResponse;
}else{// send payload to stdin of newly forked process
int fdIn = -1;
int fdOut = -1;
int fdErr = 2;
char *argv[3];
argv[0] = (char *)value.c_str();
argv[1] = (char *)trigger.c_str();
argv[2] = NULL;
pid_t myProc = Util::Procs::StartPiped(argv, &fdIn, &fdOut, 0); // start new process and return stdin file desc.
if (fdIn == -1 || fdOut == -1){// verify fdIn
FAIL_MSG("StartPiped returned invalid fd");
setenv("MIST_TRIGGER", trigger.c_str(), 1);
setenv("MIST_TRIG_DEF", defaultResponse.c_str(), 1);
pid_t myProc = Util::Procs::StartPiped(argv, &fdIn, &fdOut, &fdErr); // start new process and return stdin file desc.
unsetenv("MIST_TRIGGER");
unsetenv("MIST_TRIG_DEF");
if (fdIn == -1 || fdOut == -1 || myProc == -1){
FAIL_MSG("Could not execute trigger executable: %s", strerror(errno));
submitTriggerStat(trigger, tStartMs, false);
return defaultResponse;
}
write(fdIn, payload.data(), payload.size());
@ -87,13 +108,16 @@ namespace Triggers{
fclose(outFile);
free(fileBuf);
close(fdOut);
if (counter >= 150){
if (counter >= 150 && !ret.size()){
WARN_MSG("Using default trigger response: %s", defaultResponse.c_str());
submitTriggerStat(trigger, tStartMs, false);
return defaultResponse;
}
submitTriggerStat(trigger, tStartMs, true);
return ret;
}
close(fdOut);
submitTriggerStat(trigger, tStartMs, true);
return defaultResponse;
}
}