Reimplementing CPU discovery

First, let's try to do something that is already available in recent Zabbix agents—discovering CPUs. We do this both because it could be useful if you have a system running an old agent and because it shows how straightforward LLD can be on occasion. To do this, let's consider the following script:

for cpu in $(ls -d /sys/devices/system/cpu/cpu[0-9]*/); do 
    cpui=${cpu#/sys/devices/system/cpu/cpu} 
    [[ $(cat ${cpu}/online 2>/dev/null) ==    1 || ! -f ${cpu}/online]] &&    status=online || status=offline;    cpulist=$cpulist,'{{#CPU.NUMBER}:'${cpui%/}',    {#CPU.STATUS}:'$status'}' 
done 
echo '{data:['${cpulist#,}']}' 

It relies on /sys/devices/system/cpu/ holding a directory for each CPU, named cpu, followed by the CPU number. In each of those directories, we look for the online file—if that file is there, we check the contents. If the contents are 1, the CPU is considered to be online; if something else, it is considered to be offline. In some cases, changing the online state for CPU0 will not be allowed—this file would then be missing, and we would interpret that as the CPU being online. We then append {#CPU.NUMBER} and {#CPU.STATUS} macros with proper values and eventually print it all out, wrapped in the LLD data array. Let's use this as a user parameter.

We explored user parameters in Chapter 10, Advanced Item Monitoring.

We will concatenate it all in a single line, as we don't need a wrapper script for this command. In the Zabbix agent daemon configuration file on A test host, add the following:

UserParameter=reimplementing.cpu.discovery,for cpu in $(ls -d /sys/devices/system/cpu/cpu[0-9]*/); do cpui=${cpu#/sys/devices/system/cpu/cpu}; [[ $(cat ${cpu}/online 2>/dev/null) == 1 || ! -f ${cpu}/online ]] && status=online || status=offline; cpulist=$cpulist,'{{#CPU.NUMBER}:'${cpui%/}',{#CPU.STATUS}:'$status'}'; done; echo '{data:['${cpulist#,}']}' 
For more complicated cases or production implementation, consider a proper JSON implementation, such as the JSON::XS Perl module.

Restart the agent daemon and, on the same system, run this:

$ zabbix_get -s 127.0.0.1 -k reimplementing.cpu.discovery

On a quad-core system, it would return something similar to this:

{data:[{{#CPU.NUMBER}:0,{#CPU.STATUS}:online},{{#CPU.NUMBER}:1,{#CPU.STATUS}:online},{{#CPU.NUMBER}:2,{#CPU.STATUS}:offline},{{#CPU.NUMBER}:3,{#CPU.STATUS}:online}]} 
You can reformat JSON for better readability using Perl or Python; we did that earlier in this chapter.

We can now use this item key for an LLD rule the same way as with the built-in item. The item prototypes would work exactly the same way, and we wouldn't even need to use different LLD macros.

On most Linux systems, you can test this by bringing some CPUs or cores offline—for example, the following will bring the second CPU offline:

# echo 0 > /sys/devices/system/cpu/cpu1/online

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.241