nagios监控squid的脚本
最近一直都在忙工作上的事情,没有时间来写点东西了,公司用的cache是squid,之前有过监控,但是nagios中看不到squid的命中率,于是就写了这么一个脚本来通过pnp4nagios查看squid的一些图。下面是脚本:
#!/bin/bash#这个脚本主要是检测squid的每分钟http的请求熟、cpu的使用率、可用的文件描述符、5min的请求命中率、5min的内存请求命中率和5min的硬盘请求命中率。#并且可以通过pnp4nagios画图。PROGNAME=`basename$0`VERSION="Version1.1"AUTHOR="zhhmj(tgariltg@gmail.com)"#DEFINESST_OK=0ST_WR=1ST_CR=2ST_UK=3#VARShostname="localhost"port=8001running=0warn_descriptors=100crit_descriptors=30warn_hits=70crit_hits=50print_version(){echo"$PROGNAME$VERSION$AUTHOR"}print_help(){echo""print_versionecho""echo"Description:"echo"Getspercentageofhitsforasquidreverseproxy"echo"Options:"echo"-h|--help"echo"Printhelpinfo."echo"-H|--hostname)"echo"Setsthehostname,defaultislocalhost"echo"-P|--port)"echo"Setstheport,defaultis8001"echo"-wd)"echo"Setsthenumberofavailablefiledescriptorstowarnat,default100"echo"-cd)"echo"Setsthenumberofavailablefiledescriptorstogocriticalat,default30"echo"-wh)"echo"Setsthepercentageofhitstowarnat,default70"echo"-ch)"echo"Setsthepercentageofhitstogocriticalat,default50"echo""echo"Example:"echo"./check_squid-H127.0.0.1-P8001-wd100-cd30-wh70-ch50"echo"WARNING-Squidisservinganaverageof7.2perminutesincestartwith655349filedescriptorsleftand0.04percentofCPUuseandHitsas64%ofallrequests"exit$ST_UK}#获取squid的信息get_status_text(){status_text=$(squidclient-h${hostname}-p${port}mgr:info2>&1)}#确保服务器回复正常is_replying(){case"$status_text"in*Denied.*)echo"Errorgettingsmetrics.(Accesscontrolonsquid?)"exit$ST_CR;;*ERROR*)echo"Errorconnectingtohost"exit$ST_CR;;esac}#下面是获取有用的信息:#Availablefiledescriptors#CPUUsage#AverageHTTPrequestsperminute#Hitsas%ofallrequestsby5min#Memoryhitsas%ofhitrequestsby5min#Diskhitsas%ofhitrequestsby5minget_statistics(){available_descriptors=$(echo"${status_text}"|grep"Availablenumberoffiledescriptors"|cut-d:-f2|sed-e's/^[\t]*//')cpu_usage=$(echo"${status_text}"|grep"CPUUsage:"|cut-d:-f2|cut-d%-f1|sed-e's/^[\t]*//')avg_http_requests=$(echo"${status_text}"|grep"AverageHTTPrequestsperminutesincestart"|cut-d:-f2|cut-d%-f1|sed-e's/^[\t]*//')all_requests_hits=$(echo"${status_text}"|grep"Hitsas%ofallrequests"|awk'{print$8}'|awk-F\.'{print$1}')memory_hits=$(echo"${status_text}"|grep"Memoryhitsas%ofhitrequests"|awk'{print$9}'|awk-F\.'{print$1}')disk_hits=$(echo"${status_text}"|grep"Diskhitsas%ofhitrequests"|awk'{print$9}'|awk-F\.'{print$1}')#buidperfdatastringperfdata="'avail_descriptors'=$available_descriptors'cpu_usage'=$cpu_usage'avg_http_requests'=$avg_http_requests'all_requests_hits'=$all_requests_hits%'memory_hits'=$memory_hits%'disk_hits'=$disk_hits%"}#报警对比的判断build_output(){ out="Squidisservinganaverageof$avg_http_requestsperminutesincestartwith$available_descriptorsfiledescriptorsleftand$cpu_usagepercentofCPUuseandHitsas$all_requests_hits%ofallrequests" if[$available_descriptors-le$crit_descriptors]||[$all_requests_hits-le$crit_hits]thenecho"CRITICAL-${out}|${perfdata}"exit$ST_CRelif[$available_descriptors-le$warn_descriptors]||[$all_requests_hits-le$warn_hits]thenecho"WARNING-${out}|${perfdata}"exit$ST_WRelseecho"OK-${out}|${perfdata}"exit$ST_OKfi}#主程序#获取参数whiletest-n"$1";docase"$1"in--help|-h)print_helpexit$ST_UK;;--version|-v)print_versionexit$ST_UK;;--hostname|-H)hostname=$2shift;;--port|-P)port=$2shift;;-wd)warn_descriptors=$2shift;;-cd)crit_descriptors=$2shift;;-wh)warn_hits=$2shift;;-ch)crit_hits=$2shift;;*)echo"Unknownargument:$1"print_helpexit$ST_UK;;esacshiftdone#sanityif[$warn_descriptors-lt$crit_descriptors]||[$warn_hits-lt$crit_hits]thenecho"Warndescriptorsmustnotbelowerthancriticalandcrithitsmustnotbelowerthanwarnhits!"print_helpfiget_status_textis_replyingget_statisticsbuild_output
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。