Skip to content

Instantly share code, notes, and snippets.

@dreamcat4
Last active February 28, 2022 05:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreamcat4/1935177aafc8bb674675 to your computer and use it in GitHub Desktop.
Save dreamcat4/1935177aafc8bb674675 to your computer and use it in GitHub Desktop.
zabbix configuration examples - monitor CPU temperature & hdd status (smart attributes)

Configure zabbix jail

The smartctl program needs direct read access to raw disk devices. To allow that, we must create a devfs ruleset to un-hide our disk devices in the zabbix jail's /dev/ folder.

# Set the jailname
jailname="zabbix"

# Set the ruleset number. Use the last 3 digits of $jail_ip
rule_num="212"

# Configure devfs rules to unhide disks
touch /etc/devfs.rules

grep "devfsrules_unhide_disks=24" /etc/devfs.rules || cat >> /etc/devfs.rules <<- EOF

[devfsrules_unhide_disks=24]
add path ad* unhide
add path da* unhide
add path cd* unhide

EOF

grep "devfsrules_jail_${jailname}=${rule_num}" /etc/devfs.rules || cat >> /etc/devfs.rules <<- EOF

[devfsrules_jail_${jailname}=${rule_num}]
add include \$devfsrules_jail
add include \$devfsrules_unhide_disks

EOF

# Restart devfs to load the new ruleset
service devfs restart

# Stop the jail
qjail stop "$jailname"

# Configure the jail to use our new devfs ruleset
qjail config -b "$rule_num" "$jailname"

Configure sudoers for zabbix user

# Start the jail
qjail start "$jailname"

# Login to our new jail as root
qjail console "$jailname"

# zabbix needs to be added to sudoers.d
cat >> /usr/local/etc/sudoers.d/zabbix <<- EOF

# Permit zabbix to run smartctl as root
zabbix ALL=(ALL) NOPASSWD: /usr/local/sbin/smartctl

EOF

Configure smartctl script

# Add /usr/local/[s]bin to $PATH
echo "export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:" >> /etc/rc.conf

# Restart zabbix_agentd
service zabbix_agentd restart

# Update local pkgng database, to avoid 'failed checksum' for 'pkg install'
pkg update -f

# Install smartctl
ASSUME_ALWAYS_YES="yes" pkg install smartmontools

# Test smartctl
smartctl -a /dev/ada0

# Write 'smartparam' script
fetch -q -o /usr/local/bin/smartparam https://gist.githubusercontent.com/dreamcat4/909f7483e4ae63cd2e50/raw/f5363243e5fe87404fa64deff79288d5664f25be/smartparam
chmod +x /usr/local/bin/smartparam

Configure items and triggers

# Configure items, line1=key, line2=interval(seconds)
system.run[echo $PATH]
3600
system.run[sysctl dev.cpu.0.temperature | cut -d ' ' -f 2 | tr -d C]
30
system.run[smartparam ada0 Load_Cycle_Count]
600
system.run[smartparam ada0 Power_Cycle_Count]
3600
system.run[smartparam ada0 Power_On_Hours]
3600
system.run[smartparam ada0 Temperature_Celsius]
60
system.run[smartparam ada0 highest_error]
3600

# Configure triggers, line1=description, line2=expression
CPU Temperature Exceeded 98.0C
{Template OS FreeBSD Custom:system.run[sysctl dev.cpu.0.temperature | cut -d ' ' -f 2 | tr -d C].last()}>98.0

ada0 has exceeded 10 Load Cycles per day
{Template OS FreeBSD Custom:system.run[smartparam ada0 Load_Cycle_Count].delta(1d)}>10

ada0 disk errors != 0. This disk has encountered an error
{Template OS FreeBSD Custom:system.run[smartparam ada0 highest_error].last()}#0

ada0 Max temperature exceeded (83 C)
{Template OS FreeBSD Custom:system.run[smartparam ada0 Temperature_Celsius].last()}>83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment