nicolargo / glances

Glances an Eye on your system. A top/htop alternative for GNU/Linux, BSD, Mac OS and Windows operating systems.
http://nicolargo.github.io/glances/
Other
26.68k stars 1.53k forks source link

Make all results from amps plugins exportable #2394

Open amard33p opened 1 year ago

amard33p commented 1 year ago

Ref issue: https://github.com/nicolargo/glances/issues/1556 Currently amps plugins are not exportable: https://github.com/nicolargo/glances/blob/13f6db08bc6c9023e44d9635d69e09b2cf2a51bb/glances/exports/glances_export.py#L26-L31

At work we require lot of custom metrics and AMPs are great for that usecase. It would be great if we could export the results of AMP scripts. Here's the proposal: A single AMP script may export single or multiple metrics. For a single metric, the output CSV should only contain the column amps_AMP_name_result. Example:

[amp_conntrack]
enable=false
refresh=30
one_line=false
command=sysctl net.netfilter.nf_conntrack_count

Expected output CSV:

amps_conntrack_result
100

Now if the AMP script result contains multiple fields, it needs to output those fields in a predefined format. Example format: value_1,100;value_2,400

To implement this we can probably create a new attribute for AMP like result_fields. Example:

[amp_multi_field_amp]
enable=true
refresh=15
one_line=false
command=/usr/local/bin/mycommand.sh
result_fields=value_1,value_2

Expected output CSV:

amps_multi_field_amp_value_1, amps_multi_field_amp_value_2
100, 400
nicolargo commented 1 year ago

Hi @amard33p

what is the output format of /usr/local/bin/mycommand.sh (when it ran from the command line) ?

amard33p commented 1 year ago

@nicolargo Here's an example:

#!/bin/bash
# Example amp script which exports multiple values
# /usr/local/bin/mycommand.sh
nf_conntrack_count=$(sysctl net.netfilter.nf_conntrack_count | cut -d'=' -f2 | xargs)
nf_conntrack_max=$(sysctl net.netfilter.nf_conntrack_max | cut -d'=' -f2 | xargs)
echo "nf_conntrack_count,$nf_conntrack_count; nf_conntrack_max,$nf_conntrack_max"

Output: nf_conntrack_count,681; nf_conntrack_max,1835008

Again, this is just my idea...we can discuss on a more optimal output format.

nicolargo commented 1 year ago

Ok i understand, let me reformulate: you want a way to configure AMP plugin output in order to export it in a key=value format.

I propose to implement the following behavors:

Output format of the AMP script is a JSON

Ex: output of myamp2.sh

{'key1': ''value1", key2": "value2"}

Glances configuration file:

[amp_myamp2]
enable=true
refresh=15
one_line=false
command=/usr/local/bin/myamp1.sh
output_format=json

Expected output CSV for the AMP plugin:

amps_myamp1_key1, amps_myamp1_key1
value1, value2

Note: another fields will also be added as meta data for the AMP.

Output format of the AMP script is a CSV

Ex: output of myamp1.sh

key1,key2
value1,value2

Glances configuration file:

[amp_myamp1]
enable=true
refresh=15
one_line=false
command=/usr/local/bin/myamp1.sh
output_format=csv

Expected output CSV for the AMP plugin:

amps_myamp1_key1, amps_myamp1_key1
value1, value2

Note: another fields will also be added as meta data for the AMP.

Additional information

Just for the record, what you want to do in your amp_conntrack example already exist as a non AMP plugin:

glances --stdout-csv connections --enable-plugin connections
connections.net_connections_enabled,connections.nf_conntrack_enabled,connections.LISTEN,connections.ESTABLISHED,connections.SYN_SENT,connections.SYN_RECV,connections.initiated,connections.terminated,connections.nf_conntrack_count,connections.nf_conntrack_max,connections.nf_conntrack_percent
True,True,39,15,0,0,0,0,250.0,262144.0,0.095367431640625
...
amard33p commented 1 year ago

@nicolargo Overall I do like your idea to specify the output format but wanted to point out that outputting JSON in bash is not straightforward. This is the easiest way I found. We should mention this in the docs when the feature becomes available. printf '{"key1":"%s","key2":"%s"}\n' "$val1" "$val2"

Also I used the amp_conntrack just as an example...glad to know it's supported natively.

nicolargo commented 4 months ago

First part of the job done on the issue2394 branch.

AMPs export is enable. Not the behavor regarding the AMPs script output.