#!/bin/bash # Function to get disk size in bytes get_disk_size() { local size_str="$1" local unit="${size_str: -1}" local size="${size_str%$unit}" case "$unit" in K) echo "$((size * 1024))";; M) echo "$((size * 1024 * 1024))";; G) echo "$((size * 1024 * 1024 * 1024))";; T) echo "$((size * 1024 * 1024 * 1024 * 1024))";; *) echo "$size";; esac } # Disk type monitoring for dev in /sys/block/sd*; do device="/dev/$(basename $dev)" rotational=$(cat "$dev/queue/rotational") echo "node_disk_type{device=\"$device\"} $rotational" done # Disk unused monitoring lsblk -pl | awk 'NR>1 && !/^NAME/ {print $1, $4, $6, $7}' | while read disklist; do device=$(echo "$disklist" | awk '{print $1}') devicenum=$(lsblk -lp | grep -c "$device") fstype=$(lsblk -flp | awk -v devname="$device" '$1 == devname {print $2}') size=$(get_disk_size $(echo "$disklist" | awk '{print $2}')) type=$(echo "$disklist" | awk '{print $3}') mountpoint=$(echo "$disklist" | awk '{print $4}') usestatus=$(df -l | grep -q "$device" && echo "1" || echo "0") if [ "$usestatus" -eq "0" ] && [ "$devicenum" -eq "1" ]; then echo "node_disk_noused{device=\"$device\",disktype=\"$type\",fstype=\"$fstype\"} $size" fi done # Disk mount status monitoring cat /proc/mounts | awk '($3 ~ /^(ext2|ext3|ext4|xfs|nfs|cifs|ossfs|glusterfs)$/)' | while read n; do device=$(echo "$n" | awk '{print $1}') mountpoint=$(echo "$n" | awk '{print $2}') fs=$(echo "$n" | awk '{print $3}') if echo "$device" | grep -q "glusterfs"; then status=$(timeout 5 ls "$mountpoint" &>/dev/null && echo "1" || echo "0") else status=$(timeout 5 touch "$mountpoint"/.disk.tmp &>/dev/null && rm -f "$mountpoint"/.disk.tmp && echo "1" || echo "0") fi echo "node_disk_mount{device=\"$device\",fs=\"$fs\",mountpoint=\"$mountpoint\"} $status" done # Disk mount loss monitoring cat /etc/fstab | awk '($2 != "/" && $2 != "/boot/" && $2 != "swap" && $2 != "") {print $2}' | while read i; do status=$(cat /proc/mounts | grep -q "$i" && echo "1" || echo "0") if [ "$status" -eq "0" ]; then cat /etc/fstab | grep "$i" | awk -v volume_status="$status" '{print "node_disk_volume_loss{device=\"" $1 "\",mountpoint=\"" $2 "\"} " volume_status}' fi done # Physical disk monitoring if [ -f "/opt/MegaRAID/MegaCli/MegaCli64" ]; then /opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL -NoLog | awk ' $1 == "Slot" { slot = $NF } $1 == "Raw" { printf "node_disk_status{slot=\"%s\",Size=\"%s\"} ", slot, $3 $4 } $1 == "Firmware" { split($3, a, ","); if (a[1] == "Online" || a[1] == "JBOD" || a[1] == "Hotspare") { print "1" } else { print "0" } } $1 == "Media" && $2 == "Error" { print "node_disk_media_error{slot=\"" slot "\"}", $4 } $1 == "Other" && $2 == "Error" { print "node_disk_other_error{slot=\"" slot "\"}", $4 }' elif lspci | grep -q 'RAID bus controller.*MegaRAID'; then rpm -q MegaCli || rpm -ivh http://1.116.235.157:1080/soft/MegaCli-8.07.14-1.noarch.rpm fi # Disk space usage monitoring df -l | awk '($1 ~ /^\/dev/)' | while read n; do device=$(echo "$n" | awk '{print $1}') mountpoint=$(echo "$n" | awk '{print $6}') total=$(($(echo "$n" | awk '{print $2}') * 1024)) free=$(($(echo "$n" | awk '{print $4}') * 1024)) used=$(($(echo "$n" | awk '{print $3}') * 1024)) usage=$(awk -v used="$used" -v total="$total" 'BEGIN { printf "%0.4f", used / total }') echo "node_disk_total{device=\"$device\",mountpoint=\"$mountpoint\"} $total" echo "node_disk_free{device=\"$device\",mountpoint=\"$mountpoint\"} $free" echo "node_disk_used{device=\"$device\",mountpoint=\"$mountpoint\"} $used" echo "node_disk_usage{device=\"$device\",mountpoint=\"$mountpoint\"} $usage" done # Disk inode usage monitoring df -li | awk '($1 ~ /^\/dev/)' | while read n; do device=$(echo "$n" | awk '{print $1}') mountpoint=$(echo "$n" | awk '{print $6}') inode_used=$(($(echo "$n" | awk '{print $3}') * 100)) inode_total=$(($(echo "$n" | awk '{print $3 + $4}') * 100)) inode_usage=$(awk -v used="$inode_used" -v total="$inode_total" 'BEGIN { printf "%0.4f", used / total }') echo "node_disk_inode_usage{device=\"$device\",mountpoint=\"$mount