Skip to content

Commit

Permalink
Add: script run duration stats per host
Browse files Browse the repository at this point in the history
For testing, run the following script.
Stats are collected only if the log_whole_attack scanner preference is set.
Later, the script must run to generate the result.

```

if(description)
{
  script_oid("1.3.6.1.4.1.25623.1.200.1");
  script_version("2024-11-29T13:05:23+0000");
  script_tag(name:"last_modification", value:"2024-11-29 13:05:23 +0000 (Fri, 29 Nov 2024)");
  script_tag(name:"creation_date", value:"2024-11-29 13:05:23 +0100 (Fri, 29 Nov 2024)");
  script_tag(name:"cvss_base_vector", value:"AV:N/AC:L/Au:N/C:N/I:N/A:N");
  script_tag(name:"cvss_base", value:"0.0");
  script_name("Script Stats");
  script_copyright("Copyright (C) 2024 Greenbone AG");
  script_family("Service detection");
  # nb: Needs to run at the end of the scan because of the required info only available in this phase...
  script_category(ACT_END);
  script_tag(name:"summary", value:"This scripts logs the run duration of the scripts.");

  script_timeout(3600);
  exit(0);
}

log_message( data: collect_host_stats() );
exit( 0 );
```
  • Loading branch information
jjnicola committed Nov 29, 2024
1 parent c38c5bf commit 54baad9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
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

0 comments on commit 54baad9

Please sign in to comment.