/ proc / meminfo + gawk = JSON pratique pour les métriques de découverte dans zabbix

Tout en travaillant sur une tâche, il était nécessaire d'ajouter à la surveillance de tous les compteurs de mémoire de / proc / meminfo de plusieurs hôtes Linux, pour suivre l'état de la mémoire au fil du temps





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:~#
      
      







Un ensemble de compteurs devrait arriver à la surveillance automatiquement en utilisant la découverte directement à partir du fichier / proc / meminfo





Après la reconnaissance et la création de métriques, les données sur les métriques devraient être suivies une fois par minute en une seule opération, afin de réduire l'effet de l'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 \ "}", séparateur, $ 1, b; separator = "," ;}





Une fois que toutes les lignes ont été traitées, gawk fait END { print " ]}"



ce qu'il ferme le JSON et finalise.





De manière aussi simple et sans scripts, vous pouvez effectuer la détection et la formation de JSON avec des données en ajoutant des métriques à la surveillance et en obtenant le maximum d'effet de la facilité d'utilisation.








All Articles