#915 Add last_cmd to GET /clients API

"last_cmd" json object contains information of the last command executed
by the correspondent client. For now, it only includes "result" string
property, which stores "success" if the last command finished correctly
or "failure" on the contrary.

To populate "result" property, this commit also adds "last_cmd_result"
enum attribute to og_client struct. Client response processing fills
this attribute according to its success.

Clients in WOL_SENT state always have last_cmd->result = "unknown".

Request: GET /clients

Response: 200 OK
{
  "clients": [
    {
      "addr": "10.141.10.102",
      "state": "WOL_SENT",
      "last_cmd": {
        "result": "unknown"
      }
    },
    {
      "addr": "10.141.10.101",
      "state": "OPG",
      "speed": 1000,
      "last_cmd": {
        "result": "success"
      }
    },
    {
      "addr": "10.141.10.100",
      "state": "OPG",
      "speed": 1000,
      "last_cmd": {
        "result": "failure"
      }
    }
  ]
}
master
Javier Sánchez Parra 2022-05-10 17:18:15 +02:00
parent f03425e6ae
commit df5161ebc3
3 changed files with 45 additions and 2 deletions

View File

@ -768,6 +768,11 @@ int og_agent_state_process_response(struct og_client *cli)
break;
}
if (success)
cli->last_cmd_result = OG_SUCCESS;
else
cli->last_cmd_result = OG_FAILURE;
if (code != 200 && code != 103) {
og_dbi_update_action(cli->last_cmd_id, success);
cli->last_cmd_id = 0;
@ -831,6 +836,7 @@ int og_agent_state_process_response(struct og_client *cli)
if (err < 0) {
err = 0;
success = false;
cli->last_cmd_result = OG_FAILURE;
/* ... cancel pending actions related to this task for this client here */
}

View File

@ -387,9 +387,35 @@ static int og_json_dump_clients(const char *buffer, size_t size, void *data)
return 0;
}
static const char *og_cmd_result_str_array[] = {
[OG_UNKNOWN] = "unknown",
[OG_FAILURE] = "failure",
[OG_SUCCESS] = "success",
};
static const char *og_cmd_result_str(const enum og_cmd_result result)
{
if (result > OG_SUCCESS)
return "unknown";
return og_cmd_result_str_array[result];
}
static json_t *og_json_client_cmd_result(const enum og_cmd_result result)
{
const char *result_str;
json_t *last_cmd;
last_cmd = json_object();
result_str = og_cmd_result_str(result);
json_object_set_new(last_cmd, "result", json_string(result_str));
return last_cmd;
}
static int og_json_client_append(json_t *array, struct og_client *client)
{
json_t *addr, *state, *object;
json_t *addr, *state, *last_cmd, *object;
object = json_object();
if (!object)
@ -408,6 +434,8 @@ static int og_json_client_append(json_t *array, struct og_client *client)
}
json_object_set_new(object, "state", state);
json_object_set_new(object, "speed", json_integer(client->speed));
last_cmd = og_json_client_cmd_result(client->last_cmd_result);
json_object_set_new(object, "last_cmd", last_cmd);
json_array_append_new(array, object);
return 0;
@ -416,7 +444,7 @@ static int og_json_client_append(json_t *array, struct og_client *client)
static int og_json_client_wol_append(json_t *array,
struct og_client_wol *cli_wol)
{
json_t *addr, *state, *object;
json_t *addr, *state, *last_cmd, *object;
object = json_object();
if (!object)
@ -434,6 +462,8 @@ static int og_json_client_wol_append(json_t *array,
return -1;
}
json_object_set_new(object, "state", state);
last_cmd = og_json_client_cmd_result(OG_UNKNOWN);
json_object_set_new(object, "last_cmd", last_cmd);
json_array_append_new(array, object);
return 0;

View File

@ -42,6 +42,12 @@ enum og_cmd_type {
OG_CMD_MAX
};
enum og_cmd_result {
OG_UNKNOWN = 0,
OG_FAILURE = 1,
OG_SUCCESS = 2,
};
#define OG_MSG_REQUEST_MAXLEN 131072
struct og_client {
@ -59,6 +65,7 @@ struct og_client {
enum og_client_status status;
enum og_cmd_type last_cmd;
unsigned int last_cmd_id;
enum og_cmd_result last_cmd_result;
bool autorun;
uint32_t speed;
const char *shell_output;