Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: script run duration stats per host #1774

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nasl/nasl_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static init_func libfuncs[] = {
{"script_xref", script_xref},
{"script_tag", script_tag},
{"vendor_version", nasl_vendor_version},
{"collect_host_stats", nasl_collect_host_stats},
{"update_table_driven_lsc_data", nasl_update_table_driven_lsc_data},
{"get_preference", nasl_get_preference},
{"safe_checks", safe_checks},
Expand Down
45 changes: 45 additions & 0 deletions nasl/nasl_scanner_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,51 @@ nasl_get_preference (lex_ctxt *lexic)
return retc;
}

tree_cell *
nasl_collect_host_stats (lex_ctxt *lexic)
{
tree_cell *retc;
struct script_infos *script_infos = lexic->script_infos;
kb_t kb = script_infos->key;
GString *data = g_string_new ("");
struct kb_item *stats = NULL, *stats_tmp = NULL;
int first = 1;

stats = kb_item_get_pattern (kb, "general/script_stats*");
stats_tmp = stats;

g_string_append_c (data, '[');
while (stats_tmp)
{
char **spl = g_strsplit (stats_tmp->v_str, "/", 0);
char *buf = NULL;

if (!first)
g_string_append_c (data, ',');

buf = g_strdup_printf ("{\"%s\": {\"start\": %s, \"stop\": %s}}", spl[0],
spl[1], spl[2]);

g_string_append (data, buf);
g_strfreev (spl);
g_free (buf);

stats_tmp = stats_tmp->next;
if (first)
first = 0;
}
g_string_append_c (data, ']');

kb_item_free (stats);

retc = alloc_typed_cell (CONST_STR);
retc->x.str_val = strdup (data->str);
retc->size = data->len;
g_string_free (data, TRUE);

return retc;
}

tree_cell *
nasl_vendor_version (lex_ctxt *lexic)
{
Expand Down
3 changes: 3 additions & 0 deletions nasl/nasl_scanner_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ nasl_vendor_version (lex_ctxt *);
tree_cell *
nasl_update_table_driven_lsc_data (lex_ctxt *);

tree_cell *
nasl_collect_host_stats (lex_ctxt *);

#endif
9 changes: 9 additions & 0 deletions src/pluginlaunch.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ update_running_processes (kb_t main_kb, kb_t kb)
processes[i].start.tv_sec++;
now.tv_usec += 1000000;
}

if (log_whole)
{
char *name = nvticache_get_filename (oid);
Expand All @@ -193,6 +194,14 @@ update_running_processes (kb_t main_kb, kb_t kb)
(long) ((now.tv_usec - processes[i].start.tv_usec)
/ 1000));
g_free (name);

char msg[2048];
g_snprintf (msg, sizeof (msg), "%s/%ld/%ld", oid,
(long) ((now.tv_sec * 1000)
+ (long) (now.tv_usec / 1000)),
(long) (processes[i].start.tv_sec * 1000
+ processes[i].start.tv_usec / 1000));
kb_item_push_str (kb, "general/script_stats", msg);
}
now = old_now;
do
Expand Down
Loading