While working on one task, it was necessary to add to monitoring all memory counters from / proc / meminfo from several linux hosts, to track the state of memory over time
root@server:~# cat /proc/meminfo
MemTotal: 8139880 kB
MemFree: 146344 kB
MemAvailable: 4765352 kB
Buffers: 115436 kB
Cached: 6791672 kB
SwapCached: 9356 kB
Active: 4743296 kB
Inactive: 2734088 kB
Active(anon): 2410780 kB
Inactive(anon): 340628 kB
Active(file): 2332516 kB
Inactive(file): 2393460 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 3906556 kB
SwapFree: 3585788 kB
Dirty: 804 kB
Writeback: 0 kB
AnonPages: 567172 kB
Mapped: 2294276 kB
Shmem: 2182128 kB
KReclaimable: 198800 kB
Slab: 340540 kB
SReclaimable: 198800 kB
SUnreclaim: 141740 kB
KernelStack: 7008 kB
PageTables: 90520 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 7976496 kB
Committed_AS: 5171488 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 25780 kB
VmallocChunk: 0 kB
Percpu: 24480 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 773632 kB
DirectMap2M: 7606272 kB
DirectMap1G: 2097152 kB
root@server:~#
A set of counters should come to monitoring automatically using discovery directly from the / proc / meminfo file
After reconnaissance and creation of metrics, data on metrics should come to monitoring once a minute in one operation to reduce the effect of observation.
- , , - zabbix , , gawk
.
, , :
{$PATH} gawk
:
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; LANG=en_US.UTF-8;
{$S} discovery ( gawk, BEGIN )
gawk 'BEGIN {FS=":";ORS="";print "{\"data\": [ " }{b=gensub(/ +/,"","g",gensub(/kB/,"","g",$2) );$1=gensub(/\(|\)/,"_","g",$1);printf "%s{\"{#TYPE}\": \"%s\", \"{#VALUE}\": \"%s\"}",separator, $1, b;separator = ",";} END { print " ]}" }' /proc/meminfo
{$VALUE}
gawk 'BEGIN { FS=":"; ORS = ""; print "{" } { b=gensub(/ +/,"","g",gensub(/kB/,"","g",$2) ); $1=gensub(/\(|\)/,"_","g",$1); printf "%s\"%s\":\"%s\"",separator,$1,b;separator=",";} END { print "}" }' /proc/meminfo
meminfo system.run[{$PATH} {$VALUE},wait]
system.run {$PATH} {$VALUE} ,
meminfo
meminfo
{#TYPE} [ ] VmallocTotal|VmallocChunk
, ,
- JSONPath + , .
,
, gawk JSON discovery
JSON discovery
root@server:~# gawk 'BEGIN {FS=":";ORS="";print "{\"data\": [ " }{b=gensub(/ +/,"","g",gensub(/kB/,"","g",$2) );$1=gensub(/\(|\)/,"_","g",$1);printf "%s{\"{#TYPE}\": \"%s\", \"{#VALUE}\": \"%s\"}",separator, $1, b;separator = ",";} END { print " ]}" }' /proc/meminfo
{"data": [ {"{#TYPE}": "MemTotal", "{#VALUE}": "8139880"},{"{#TYPE}": "MemFree", "{#VALUE}": "147628"},{"{#TYPE}": "MemAvailable", "{#VALUE}": "4764232"},{"{#TYPE}": "Buffers", "{#VALUE}": "115316"},{"{#TYPE}": "Cached", "{#VALUE}": "6789504"},{"{#TYPE}": "SwapCached", "{#VALUE}": "9356"},{"{#TYPE}": "Active", "{#VALUE}": "4742408"},{"{#TYPE}": "Inactive", "{#VALUE}": "2733636"},{"{#TYPE}": "Active_anon_", "{#VALUE}": "2411644"},{"{#TYPE}": "Inactive_anon_", "{#VALUE}": "340828"},{"{#TYPE}": "Active_file_", "{#VALUE}": "2330764"},{"{#TYPE}": "Inactive_file_", "{#VALUE}": "2392808"},{"{#TYPE}": "Unevictable", "{#VALUE}": "0"},{"{#TYPE}": "Mlocked", "{#VALUE}": "0"},{"{#TYPE}": "SwapTotal", "{#VALUE}": "3906556"},{"{#TYPE}": "SwapFree", "{#VALUE}": "3585788"},{"{#TYPE}": "Dirty", "{#VALUE}": "368"},{"{#TYPE}": "Writeback", "{#VALUE}": "0"},{"{#TYPE}": "AnonPages", "{#VALUE}": "568164"},{"{#TYPE}": "Mapped", "{#VALUE}": "2294960"},{"{#TYPE}": "Shmem", "{#VALUE}": "2182128"},{"{#TYPE}": "KReclaimable", "{#VALUE}": "198800"},{"{#TYPE}": "Slab", "{#VALUE}": "340536"},{"{#TYPE}": "SReclaimable", "{#VALUE}": "198800"},{"{#TYPE}": "SUnreclaim", "{#VALUE}": "141736"},{"{#TYPE}": "KernelStack", "{#VALUE}": "7040"},{"{#TYPE}": "PageTables", "{#VALUE}": "90568"},{"{#TYPE}": "NFS_Unstable", "{#VALUE}": "0"},{"{#TYPE}": "Bounce", "{#VALUE}": "0"},{"{#TYPE}": "WritebackTmp", "{#VALUE}": "0"},{"{#TYPE}": "CommitLimit", "{#VALUE}": "7976496"},{"{#TYPE}": "Committed_AS", "{#VALUE}": "5189180"},{"{#TYPE}": "VmallocTotal", "{#VALUE}": "34359738367"},{"{#TYPE}": "VmallocUsed", "{#VALUE}": "25780"},{"{#TYPE}": "VmallocChunk", "{#VALUE}": "0"},{"{#TYPE}": "Percpu", "{#VALUE}": "24480"},{"{#TYPE}": "HardwareCorrupted", "{#VALUE}": "0"},{"{#TYPE}": "AnonHugePages", "{#VALUE}": "0"},{"{#TYPE}": "ShmemHugePages", "{#VALUE}": "0"},{"{#TYPE}": "ShmemPmdMapped", "{#VALUE}": "0"},{"{#TYPE}": "FileHugePages", "{#VALUE}": "0"},{"{#TYPE}": "FilePmdMapped", "{#VALUE}": "0"},{"{#TYPE}": "CmaTotal", "{#VALUE}": "0"},{"{#TYPE}": "CmaFree", "{#VALUE}": "0"},{"{#TYPE}": "HugePages_Total", "{#VALUE}": "0"},{"{#TYPE}": "HugePages_Free", "{#VALUE}": "0"},{"{#TYPE}": "HugePages_Rsvd", "{#VALUE}": "0"},{"{#TYPE}": "HugePages_Surp", "{#VALUE}": "0"},{"{#TYPE}": "Hugepagesize", "{#VALUE}": "2048"},{"{#TYPE}": "Hugetlb", "{#VALUE}": "0"},{"{#TYPE}": "DirectMap4k", "{#VALUE}": "773632"},{"{#TYPE}": "DirectMap2M", "{#VALUE}": "7606272"},{"{#TYPE}": "DirectMap1G", "{#VALUE}": "2097152"} ]}
root@server:~#
JSON discovery
{"data": [
{"{#TYPE}": "MemTotal", "{#VALUE}": "8139880"},
{"{#TYPE}": "MemFree", "{#VALUE}": "147628"},
{"{#TYPE}": "MemAvailable", "{#VALUE}": "4764232"},
{"{#TYPE}": "Buffers", "{#VALUE}": "115316"},
{"{#TYPE}": "Cached", "{#VALUE}": "6789504"},
{"{#TYPE}": "SwapCached", "{#VALUE}": "9356"},
.....
{"{#TYPE}": "DirectMap4k", "{#VALUE}": "773632"},
{"{#TYPE}": "DirectMap2M", "{#VALUE}": "7606272"},
{"{#TYPE}": "DirectMap1G", "{#VALUE}": "2097152"} ]}
root@server:~#
root@server:~# gawk 'BEGIN {FS=":";ORS="";print "{\"data\": [ " }
{
b = gensub(/ +/,"","g", gensub(/kB/,"","g",$2) );
$1=gensub(/\(|\)/,"_","g",$1);
printf "%s{\"{#TYPE}\": \"%s\", \"{#VALUE}\": \"%s\"}",separator, $1, b;separator = ",";}
END { print " ]}" }' /proc/meminfo
BEGIN {FS=":";ORS="";print "{\"data\": [ " }
FS=":";
- $1, $2 ... $n
print "{\"data\": [ "
- ,
gawk , ":" $1 , $2 ,
- CommitLimit: 7976496 kB
:
$1=CommitLimit
$2= 7976496 kB
-
b = gensub(/ +/,"","g", gensub(/kB/,"","g",$2) );
gensub
gensub(/kB/,"","g",$2)
kB gensub(/ +/,"","g", ........ );
, .
{#TYPE} ( ) , , .
- , , _ $1=gensub(/\(|\)/,"_","g",$1);
, JSON
printf "% s {\" {# TYPE} \ ": \"% s \ ", \" {# VALUE} \ ": \"% s \ "}", separator, $ 1, b; separator = "," ;}
After all the lines have been processed, gawk does END { print " ]}"
what it closes the JSON and finalizes.
In such a simple way without scripts, you can make detection and formation of JSON with data by adding metrics to monitoring and getting the maximum effect from ease of use.