Skip to content

Commit

Permalink
Add: openvasd_get_performance()
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Mar 4, 2025
1 parent 90ae580 commit a833415
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
91 changes: 69 additions & 22 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,7 @@ parse_results (const gchar *body, GSList **results)
goto res_cleanup;

item = cJSON_GetObjectItem (result_obj, "detail");
if (item != NULL
&& cJSON_IsObject (item))
if (item != NULL && cJSON_IsObject (item))
{
cJSON *detail_obj = NULL;

Expand All @@ -1137,21 +1136,21 @@ parse_results (const gchar *body, GSList **results)
{
detail_source_type = gvm_json_obj_str (detail_obj, "type");
detail_source_name = gvm_json_obj_str (detail_obj, "name");
detail_source_description = gvm_json_obj_str (detail_obj, "description");
detail_source_description =
gvm_json_obj_str (detail_obj, "description");
}
}

result = openvasd_result_new (gvm_json_obj_double (result_obj, "id"),
gvm_json_obj_str (result_obj, "type"),
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
gvm_json_obj_int (result_obj, "port"),
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"),
detail_name, detail_value,
detail_source_type, detail_source_name,
detail_source_description);
result = openvasd_result_new (
gvm_json_obj_double (result_obj, "id"),
gvm_json_obj_str (result_obj, "type"),
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
gvm_json_obj_int (result_obj, "port"),
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"), detail_name, detail_value,
detail_source_type, detail_source_name, detail_source_description);

*results = g_slist_append (*results, result);
ret = 200;
Expand Down Expand Up @@ -1310,8 +1309,7 @@ openvasd_get_scan_progress_ext (openvasd_connector_t conn,
// read progress of single running hosts
cJSON *scanning;
scanning = cJSON_GetObjectItem (reader, "scanning");
if (scanning != NULL
&& cJSON_IsObject (scanning))
if (scanning != NULL && cJSON_IsObject (scanning))
{
cJSON *host = scanning->child;
while (host)
Expand Down Expand Up @@ -1591,6 +1589,56 @@ openvasd_get_health_started (openvasd_connector_t conn)
return response;
}

openvasd_resp_t
openvasd_get_performance (openvasd_connector_t conn,

Check warning on line 1593 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1593

Added line #L1593 was not covered by tests
openvasd_get_performance_opts_t opts)
{
openvasd_resp_t response = NULL;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;

Check warning on line 1599 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1596-L1599

Added lines #L1596 - L1599 were not covered by tests
time_t now;
time (&now);

Check warning on line 1601 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1601

Added line #L1601 was not covered by tests

response = g_malloc0 (sizeof (struct openvasd_response));

Check warning on line 1603 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1603

Added line #L1603 was not covered by tests

if (!opts.titles || !strcmp (opts.titles, "") || opts.start < 0
|| opts.start > now || opts.end < 0 || opts.end > now)
{
response->code = RESP_CODE_ERR;
response->body =
g_strdup ("{\"error\": \"Couldn't send get_performance command "

Check warning on line 1610 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1608-L1610

Added lines #L1608 - L1610 were not covered by tests
"to scanner. Bad or missing parameters.\"}");
return response;

Check warning on line 1612 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1612

Added line #L1612 was not covered by tests
}
gchar *query =
g_strdup_printf ("/health/performance?start=%d&end=%d&titles=%s",

Check warning on line 1615 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1615

Added line #L1615 was not covered by tests
opts.start, opts.end, opts.titles);
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, GET, query, NULL, customheader, &err);

Check warning on line 1618 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1617-L1618

Added lines #L1617 - L1618 were not covered by tests
if (hnd == NULL)
{
curl_slist_free_all (customheader);
response->code = RESP_CODE_ERR;
response->body = err;
return response;

Check warning on line 1624 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1621-L1624

Added lines #L1621 - L1624 were not covered by tests
}

openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);

Check warning on line 1628 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1627-L1628

Added lines #L1627 - L1628 were not covered by tests
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));

Check warning on line 1630 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1630

Added line #L1630 was not covered by tests
else if (response->code == RESP_CODE_ERR)
{
response->body = g_strdup (

Check warning on line 1633 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1633

Added line #L1633 was not covered by tests
"{\"error\": \"Not possible to get performance information.\"}");
g_warning ("%s: Not possible to get performance information", __func__);

Check warning on line 1635 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1635

Added line #L1635 was not covered by tests
}

openvasd_reset_vt_stream (conn);
return response;

Check warning on line 1639 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1638-L1639

Added lines #L1638 - L1639 were not covered by tests
}

openvasd_resp_t
openvasd_get_scan_preferences (openvasd_connector_t conn)
{
Expand Down Expand Up @@ -1809,12 +1857,11 @@ openvasd_parsed_scans_preferences (openvasd_connector_t conn, GSList **params)
}
}

param =
openvasd_param_new (g_strdup (gvm_json_obj_str (param_obj, "id")),
g_strdup (gvm_json_obj_str (param_obj, "name")),
g_strdup (defval),
g_strdup (gvm_json_obj_str (param_obj, "description")),
g_strdup (param_type), mandatory);
param = openvasd_param_new (
g_strdup (gvm_json_obj_str (param_obj, "id")),
g_strdup (gvm_json_obj_str (param_obj, "name")), g_strdup (defval),
g_strdup (gvm_json_obj_str (param_obj, "description")),

Check warning on line 1863 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1860-L1863

Added lines #L1860 - L1863 were not covered by tests
g_strdup (param_type), mandatory);
g_free (defval);
g_free (param_type);
*params = g_slist_append (*params, param);
Expand Down
10 changes: 10 additions & 0 deletions openvasd/openvasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ struct openvasd_scan_status
long response_code;
};

typedef struct
{
int start; /**< Start interval. */
int end; /**< End interval. */
char *titles; /**< Graph title. */
} openvasd_get_performance_opts_t;

typedef struct openvasd_response *openvasd_resp_t;

typedef enum OPENVASD_RESULT_MEMBER_INT openvasd_result_member_int_t;
Expand Down Expand Up @@ -182,6 +189,9 @@ openvasd_resp_t openvasd_get_health_ready (openvasd_connector_t);

openvasd_resp_t openvasd_get_health_started (openvasd_connector_t);

openvasd_resp_t openvasd_get_performance (openvasd_connector_t,
openvasd_get_performance_opts_t);

/* Scanner preferences */

typedef struct openvasd_param openvasd_param_t;
Expand Down

0 comments on commit a833415

Please sign in to comment.