#!/bin/sh
#
# macsat RRDTool script for ASUS WL-500g (Deluxe) v1.1 (18-07-2005)
#
# (c) 2005 by macsat@macsat.com
# Feel free to distribute and improve :-)
# Edited by S$rG 9.12.2008 - 2009
# (c) 2008...
# version: beta 10^(-98)
# Edited by SerGri (SerGri@yandex.ru) 23.09.2010
# (c) 2010...
# version: final 12
#
PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Configuration start
# Commands (консольные параметры)
up_g=update_graphs
up_s_g_d=update_graph_daily
up_s_g_w=update_graph_weekly
up_s_g_m=update_graph_monthly
up_s_g_y=update_graph_yearly
up_s_ip_d=update_graph_ip_daily
up_s_ip_w=update_graph_ip_weekly
up_s_ip_m=update_graph_ip_monthly
up_s_ip_y=update_graph_ip_yearly
up_s_p_d=update_graph_ports_daily
up_s_p_w=update_graph_ports_weekly
up_s_p_m=update_graph_ports_monthly
up_s_p_y=update_graph_ports_yearly
up_cpu=update_graph_cpu
up_cpu_24=update_graph_cpu_24
up_cpu_48=update_graph_cpu_48
up_cgi=update_cgi
#Константы
D_G="Daily Graphs"
W_G="Weekly Graphs"
M_G="Monthly Graphs"
Y_G="Yearly Graphs"
c_u="CPU usage"
mem_u="Memory usage"
ip_detail="Detail IPs"
#chains for traffic ip
c_t_in="in_traffic" # inbound traffic
c_t_out="out_traffic" # outbound traffic
RRDDATA=/opt/traffic # Directory for storing RRD Databases
# Directory for storing webpages / images
# and cgi scripts (creating dir "cgi-bin")
RRDIMG=/opt/share/www/rrd-new/traffic # dir for images
cgi=/opt/share/www/rrd-new/ # dir for cgi
IMG_WWW="/rrd-new/traffic" # директория отностительно $cgi
graph_w=550 # ширина графиков (min=550 ; max=950)
script="${RRDDATA}/rrd.sh" # script name
f_conf="${RRDDATA}/config" # config name
log_ip="${RRDDATA}/ip.list" # ip`s config name
log_ports="${RRDDATA}/ports.list" # ports config name
#Values for MONIT:
#задает номер правила, куда вставлены правила для статистики по ip и портам.
#По умолчанию, MONIT=2, т.к. 1-ое правило обычно - "-A FORWARD -i br0 -o br0 -j ACCEPT", т.е. локалка.
#Если вам нужна суммарная статистика (локалка+инет), то MONIT=1.
MONIT=2 # MONIT="x" input chains in "x" rule (chain FORWARD)
Monit_Ports=0 # "1" - yes; "0" - no
# Configuration End
#Output date for log...
echo "
============= Script Begin =============
"
date
if [ ! -d "${RRDDATA}" ]
then
echo "RRD Database dir: $RRDDATA does not exist...Creating Now...."
mkdir -p "${RRDDATA}"
fi
if [ ! -d "${RRDIMG}" ]
then
echo "RRD Image / web dir: $RRDIMG does not exist....Creating Now...."
mkdir -p "${RRDIMG}"
mkdir -p "${cgi}"
fi
if [ ! -f "${log_ip}" ]
then
echo "" > "${log_ip}"
fi
if [ "$Monit_Ports" -eq 1 ] && [ ! -f "${log_ports}" ]
then
echo "" > "${log_ports}"
fi
if [ ! -f "${cgi}/index.cgi" ] || [ "{$1}" = "{$up_cgi}" ]
then
echo " cgi script does not exist.....Creating Now...."
index="${cgi}/index.cgi"
echo "#!/bin/sh" > "${index}"
echo 'run_time=`cat /proc/uptime | awk '\''{print $1}'\''`' >> "${index}"
echo '. '"$f_conf" >> "${index}"
echo 'tab=""' >> "${index}"
echo "RRDDATA=$RRDDATA" >> "${index}"
echo "RRDIMG=$IMG_WWW" >> "${index}"
echo 'cnt="
"' >> "${index}"
echo 'cnt_end=""' >> "${index}"
echo 'period=" in this "' >> "${index}"
echo 'traf="Total traffic"' >> "${index}"
echo 'speed="Traffic"' >> "${index}"
echo 'syst="System"' >> "${index}"
echo 'hlp="Help"' >> "${index}"
echo 'd="day"' >> "${index}"
echo 'w="week"' >> "${index}"
echo 'm="month"' >> "${index}"
echo 'y="year"' >> "${index}"
echo 'frame_h=700' >> "${index}"
echo "up_cpu=${up_cpu}" >> "${index}"
echo "up_cpu_24=${up_cpu_24}" >> "${index}"
echo "up_cpu_48=${up_cpu_48}" >> "${index}"
echo "up_s_g_d=${up_s_g_d}" >> "${index}"
echo "up_s_g_w=${up_s_g_w}" >> "${index}"
echo "up_s_g_m=${up_s_g_m}" >> "${index}"
echo "up_s_g_y=${up_s_g_y}" >> "${index}"
echo "up_s_ip_d=${up_s_ip_d}" >> "${index}"
echo "up_s_ip_w=${up_s_ip_w}" >> "${index}"
echo "up_s_ip_m=${up_s_ip_m}" >> "${index}"
echo "up_s_ip_y=${up_s_ip_y}" >> "${index}"
echo "up_s_p_d=${up_s_p_d}" >> "${index}"
echo "up_s_p_w=${up_s_p_w}" >> "${index}"
echo "up_s_p_m=${up_s_p_m}" >> "${index}"
echo "up_s_p_y=${up_s_p_y}" >> "${index}"
echo 'd_g="Daily graphs"' >> "${index}"
echo 'w_g="Weekly graphs"' >> "${index}"
echo 'm_g="Monthly graphs"' >> "${index}"
echo 'y_g="Yearly graphs"' >> "${index}"
echo 'ip_detail="IPs detail"' >> "${index}"
echo "log_ip=$log_ip" >> "${index}"
echo 'if [ "$QUERY_STRING" == "" ]' >> "${index}"
echo ' then' >> "${index}"
echo ' QUERY_STRING="sys"' >> "${index}"
echo 'fi' >> "${index}"
echo 'case "$QUERY_STRING" in' >> "${index}"
echo 'sys) vhead="${syst}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab="`cat /proc/version`
`uptime`
CPU`cat /proc/cpuinfo`
"' >> "${index}"
echo ' tab="${tab}Memory`cat /proc/meminfo`
"' >> "${index}"
echo ' tab="${tab}Filesystem`df -h`
"' >> "${index}"
echo ' tab="${tab}Proccess`ps`
"' >> "${index}"
echo ' tab="${tab}Route`route -en`
"' >> "${index}"
echo ' tab="${tab}ARP`cat /proc/net/arp`
"' >> "${index}"
echo ' tab="${tab}Hosts`cat /etc/hosts`
"' >> "${index}"
echo ' tab="${tab}Interfaces`ifconfig`
" ;;' >> "${index}"
echo 'iptab) vhead="${syst}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab=""`iptables-save`"
" ;;' >> "${index}"
echo 'connect) vhead="${syst}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab="`netstat -a`
" ;;' >> "${index}"
echo 'cpu_mem) vhead="CPU & Memory usage in this day"' >> "${index}"
echo ' UPG="${up_cpu}"' >> "${index}"
echo ' tab="
" ;;' >> "${index}"
echo 'cpu_mem_24) vhead="CPU & Memory usage in last 24 hours"' >> "${index}"
echo ' UPG="${up_cpu_24}"' >> "${index}"
echo ' tab="
" ;;' >> "${index}"
echo 'cpu_mem_48) vhead="CPU & Memory usage in last 48 hours"' >> "${index}"
echo ' UPG="${up_cpu_48}"' >> "${index}"
echo ' tab="
" ;;' >> "${index}"
echo 's_g_d) vhead=${speed}${period}${d}' >> "${index}"
echo ' UPG=${up_s_g_d}' >> "${index}"
echo ' graph1="wan${d}"' >> "${index}"
echo ' graph2="lan${d}"' >> "${index}"
echo ' graph3="wlan${d}" ;;' >> "${index}"
echo 's_g_w) vhead=${speed}${period}${w}' >> "${index}"
echo ' UPG=${up_s_g_w}' >> "${index}"
echo ' graph1="wan${w}"' >> "${index}"
echo ' graph2="lan${w}"' >> "${index}"
echo ' graph3="wlan${w}" ;;' >> "${index}"
echo 's_g_m) vhead=${speed}${period}${m}' >> "${index}"
echo ' UPG=${up_s_g_m}' >> "${index}"
echo ' graph1="wan${m}"' >> "${index}"
echo ' graph2="lan${m}"' >> "${index}"
echo ' graph3="wlan${m}" ;;' >> "${index}"
echo 's_g_y) vhead=${speed}${period}${y}' >> "${index}"
echo ' UPG=${up_s_g_y}' >> "${index}"
echo ' graph1="wan${y}"' >> "${index}"
echo ' graph2="lan${y}"' >> "${index}"
echo ' graph3="wlan${y}" ;;' >> "${index}"
echo 's_g_ip_d) vhead="${ip_detail} - ${d}"' >> "${index}"
echo ' UPG="${up_s_ip_d}"' >> "${index}"
echo ' for ip_v in $ip_l' >> "${index}"
echo ' do' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done ;;' >> "${index}"
echo 's_g_ip_w) vhead="${ip_detail} - ${w}"' >> "${index}"
echo ' UPG="${up_s_ip_w}"' >> "${index}"
echo ' for ip_v in $ip_l' >> "${index}"
echo ' do' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done ;;' >> "${index}"
echo 's_g_ip_m) vhead="${ip_detail} - ${m}"' >> "${index}"
echo ' UPG="${up_s_ip_m}"' >> "${index}"
echo ' for ip_v in $ip_l' >> "${index}"
echo ' do' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done ;;' >> "${index}"
echo 's_g_ip_y) vhead="${ip_detail} - ${y}"' >> "${index}"
echo ' UPG="${up_s_ip_y}"' >> "${index}"
echo ' for ip_v in $ip_l' >> "${index}"
echo ' do' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done ;;' >> "${index}"
if [ "$Monit_Ports" -eq 1 ]
then
echo 's_g_p_d) vhead="${speed}${period}${d}"' >> "${index}"
echo ' UPG="${up_s_p_d}"' >> "${index}"
echo ' for v_p in $ports_l' >> "${index}"
echo ' do' >> "${index}"
echo ' v_p=`echo $v_p | sed ''s/:/_/''`' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done' >> "${index}"
echo ' tab="${tab}
" ;;' >> "${index}"
echo 's_g_p_w) vhead="${speed}${period}${w}"' >> "${index}"
echo ' UPG="${up_s_p_w}"' >> "${index}"
echo ' for v_p in $ports_l' >> "${index}"
echo ' do' >> "${index}"
echo ' v_p=`echo $v_p | sed ''s/:/_/''`' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done' >> "${index}"
echo ' tab="${tab}
" ;;' >> "${index}"
echo 's_g_p_m) vhead="${speed}${period}${m}"' >> "${index}"
echo ' UPG="${up_s_p_m}"' >> "${index}"
echo ' for v_p in $ports_l' >> "${index}"
echo ' do' >> "${index}"
echo ' v_p=`echo $v_p | sed ''s/:/_/''`' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done' >> "${index}"
echo ' tab="${tab}
" ;;' >> "${index}"
echo 's_g_p_y) vhead="${speed}${period}${y}"' >> "${index}"
echo ' UPG="${up_s_p_y}"' >> "${index}"
echo ' for v_p in $ports_l' >> "${index}"
echo ' do' >> "${index}"
echo ' v_p=`echo $v_p | sed ''s/:/_/''`' >> "${index}"
echo ' tab="${tab}
"' >> "${index}"
echo ' done' >> "${index}"
echo ' tab="${tab}
" ;;' >> "${index}"
fi
echo 'help) vhead="${hlp}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab="" ;;' >> "${index}"
echo 'help1) vhead="${hlp}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab="" ;;' >> "${index}"
echo 'help2) vhead="${hlp}"' >> "${index}"
echo ' cnt=""' >> "${index}"
echo ' cnt_end=""' >> "${index}"
echo ' tab="" ;;' >> "${index}"
echo '*) vhead="${syst}"' >> "${index}"
echo ' tab="Hi!" ;;' >> "${index}"
echo 'esac' >> "${index}"
echo 'if [ "$tab" == "" ] & [ "$graph3" != "" ]' >> "${index}"
echo ' then' >> "${index}"
echo ' tab="
"' >> "${index}"
echo 'fi' >> "${index}"
echo 'echo "Content-type: text/html; charset=utf-8"' >> "${index}"
echo 'echo ' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo "System & Traffic"' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo "${vhead} | "' >> "${index}"
echo 'echo " Update data "' >> "${index}"
echo 'echo " |
"' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
echo 'echo ""' >> "${index}"
if [ "$Monit_Ports" -eq 1 ]
then
echo 'echo ""' >> "${index}"
fi
echo 'echo " | "' >> "${index}"
echo 'echo "${cnt} ${tab} ${cnt_end} |
"' >> "${index}"
echo 'run_time2=`cat /proc/uptime | awk '\''{print $1}'\''`' >> "${index}"
echo 'run_time=`echo "$run_time2 $run_time" | awk '\''{printf "%s\n",$1-$2}'\''`' >> "${index}"
echo 'echo "${run_time} sec."' >> "${index}"
echo 'echo ""' >> "${index}"
chmod +x "${index}"
fi
if [ ! -f "${cgi}/update.cgi" ] || [ "{$1}" = "{$up_cgi}" ]
then
echo " cgi script does not exist.....Creating Now...."
echo "#!/bin/sh" > "${cgi}/update.cgi"
echo "rrd_path=$script" >> "${cgi}/update.cgi"
echo 'echo "Content-type: text/html; charset=utf-8"' >> "${cgi}/update.cgi"
echo "echo " >> "${cgi}/update.cgi"
echo 'echo "Update"' >> "${cgi}/update.cgi"
echo 'if [ "$QUERY_STRING" != "" ]' >> "${cgi}/update.cgi"
echo ' then' >> "${cgi}/update.cgi"
echo ' $rrd_path $QUERY_STRING &' >> "${cgi}/update.cgi"
echo ' wait' >> "${cgi}/update.cgi"
echo ' else' >> "${cgi}/update.cgi"
echo ' echo "
Nothing update!
"' >> "${cgi}/update.cgi"
echo 'fi' >> "${cgi}/update.cgi"
echo 'echo "
Go back"' >> "${cgi}/update.cgi"
chmod +x "${cgi}/update.cgi"
fi
echo "
Create started...
"
if [ ! -f "$f_conf" ]
then
WANIF=`nvram get wan_ifname`
LANIF=`nvram get lan_ifnames | awk '{print $1}'`
WLANIF=`nvram get wl0_ifname`
WLINT=`nvram get lan_ifname`
echo "WANIF=$WANIF" > $f_conf
echo "LANIF=$LANIF" >> $f_conf
echo "WLANIF=$WLANIF" >> $f_conf
echo "WLINT=$WLINT" >> $f_conf
else
. $f_conf
fi
#debug lines
echo "WAN Interface --> ${WANIF}
"
echo "LAN Interface --> ${LANIF}
"
echo "WLAN Interface --> ${WLANIF}
"
WANRRD="${RRDDATA}/${WANIF}.rrd"
LANRRD="${RRDDATA}/${LANIF}.rrd"
WLANRRD="${RRDDATA}/${WLANIF}.rrd"
CPU_D="${RRDDATA}/cpu.rrd"
MEM_D="${RRDDATA}/mem.rrd"
CreateRRD ()
{
rrdtool create "${1}" \
DS:in:DERIVE:600:0:U \
DS:out:DERIVE:600:0:U \
RRA:AVERAGE:0.5:1:2018 \
RRA:AVERAGE:0.5:4:2232 \
RRA:AVERAGE:0.5:12:8784
}
CreateRRD_CPU ()
{
rrdtool create "${1}" \
DS:user:COUNTER:600:0:U \
DS:system:COUNTER:600:0:U \
DS:nice:COUNTER:600:0:U \
DS:idle:COUNTER:600:0:U \
RRA:AVERAGE:0.5:1:576
}
CreateRRD_Mem ()
{
rrdtool create "${1}" \
DS:ram:GAUGE:600:0:U \
DS:usable:GAUGE:600:0:U \
DS:kernel_code:GAUGE:600:0:U \
DS:kernel_data:GAUGE:600:0:U \
DS:free:GAUGE:600:0:U \
DS:buff:GAUGE:600:0:U \
DS:cache:GAUGE:600:0:U \
DS:swap_free:GAUGE:600:0:U \
DS:swap_total:GAUGE:600:0:U \
RRA:AVERAGE:0.5:1:576
}
if [ ! -f "${WANRRD}" ]
then
echo "RRD file : ${WANRRD} does not exist...Creating Now..."
CreateRRD "${WANRRD}"
fi
if [ ! -f "${LANRRD}" ]
then
echo "RRD file : ${LANRRD} does not exist...Creating Now..."
CreateRRD "${LANRRD}"
fi
if [ ! -f "${WLANRRD}" ]
then
echo "RRD file : ${WLANRRD} does not exist...Creating Now..."
CreateRRD "${WLANRRD}"
fi
if [ ! -f "${CPU_D}" ]
then
echo "RRD file : ${CPU_D} does not exist...Creating Now..."
CreateRRD_CPU "${CPU_D}"
fi
if [ ! -f "${MEM_D}" ]
then
echo "RRD file : ${MEM_D} does not exist...Creating Now..."
CreateRRD_Mem "${MEM_D}"
fi
#3-и массива ip_s - как есть в файле,ip - [ip]/[mask] и ip_l - имена файлов.rrd
get_ip ()
{
ip=""
ip_l=""
ip_s=`cat "${log_ip}"`
for ip_ in $ip_s
do
a=${ip_%/*}
v_a=`echo $a | sed 's/[0-9.]*//'`
if [ "$v_a" = "" ]
then
b=${ip_#*/}
v_a=`echo $b | sed 's/[0-9.]*//g'`
if [ "$v_a" = "" ]
then
[ "$a" = "$b" ] && b='32'
[ "${#b}" -gt 2 ] && b=" $b" || b="/$b"
c=`ipcalc -pnm "$a$b"`
a=`echo ${c#*NETWORK=} | awk '{print $1}'`
b=`echo ${c#*NETMASK=} | awk '{print $1}'`
c=`echo ${c#*PREFIX=} | awk '{print $1}'`
ip_l="$ip_l $a"_"$c"
[ "$b" = "255.255.255.255" ] && ip="$ip $a" || ip="$ip $a/$b"
fi
fi
ip=`echo $ip | sed 's/^[ \t]*//;s/[ \t]*$//'`
ip_l=`echo $ip_l | sed 's/^[ \t]*//;s/[ \t]*$//'`
done
}
# $1 - port/ports [xxxx:xxxx]
ports_rules_wl ()
{
for v_p in $ports_l
do
iptables -A "$c_t_in" -p tcp -m tcp --sport $v_p
iptables -A "$c_t_in" -p udp -m udp --sport $v_p
iptables -A "$c_t_out" -p tcp -m tcp --dport $v_p
iptables -A "$c_t_out" -p udp -m udp --dport $v_p
done
iptables -A $c_t_in -p tcp -m tcp -f
iptables -A $c_t_in -p udp -m udp -f
iptables -A $c_t_out -p tcp -m tcp -f
iptables -A $c_t_out -p udp -m udp -f
iptables -A $c_t_in -p icmp -m icmp
iptables -A $c_t_out -p icmp -m icmp
iptables -A $c_t_in -p icmp -m icmp -f
iptables -A $c_t_out -p icmp -m icmp -f
}
if [ "$1" = "" ]
then
WANIN=`ifconfig "${WANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
WANOUT=`ifconfig "${WANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
LANOUT=`ifconfig "${LANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
LANIN=`ifconfig "${LANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
WLANOUT=`ifconfig "${WLANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
WLANIN=`ifconfig "${WLANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
# Update the Databases
`rrdupdate "${WANRRD}" -t in:out N:"${WANIN}":"${WANOUT}"`
`rrdupdate "${LANRRD}" -t in:out N:"${LANIN}":"${LANOUT}"`
`rrdupdate "${WLANRRD}" -t in:out N:"${WLANIN}":"${WLANOUT}"`
# for Debug interfaces utilization
echo "----------------------------------------"
echo "WAN -> in: ${WANIN} out: ${WANOUT} "
echo "LAN -> in: ${LANIN} out: ${LANOUT}"
echo "WLAN -> in: ${WLANIN} out: ${WLANOUT}"
echo "----------------------------------------"
# Collect CPU usage Stat
v_cpu=`cat /proc/stat | grep "^cpu "`
cpu_user=`echo "$v_cpu" | awk '{print $2}'`
cpu_nice=`echo "$v_cpu" | awk '{print $3}'`
cpu_system=`echo "$v_cpu" | awk '{print $4}'`
cpu_idle=`echo "$v_cpu" | awk '{print $5}'`
`rrdupdate "${CPU_D}" -t user:system:nice:idle N:"${cpu_user}":"${cpu_system}":"${cpu_nice}":"${cpu_idle}"`
# for Debud CPU Utilisation
echo "----------------------------------------"
echo "CPU Utilisation User --> $cpu_user"
echo "CPU Utilisation Nice --> $cpu_nice"
echo "CPU Utilisation System --> $cpu_system"
echo "CPU Utilisation Iddle --> $cpu_idle"
echo "----------------------------------------"
# Collect Memory usage Stat
mem_r_b=`cat /proc/iomem | grep RAM | awk '{print $1}' | awk -F "-" '{print $1}'`
mem_r_b=`echo $((0x$mem_r_b))`
mem_r_e=`cat /proc/iomem | grep RAM | awk '{print $1}' | awk -F "-" '{print $2}'`
mem_r_e=`echo $((0x$mem_r_e))`
let mem_r=$mem_r_e-$mem_r_b
mem_kc_b=`cat /proc/iomem | grep code | awk '{print $1}' | awk -F "-" '{print $1}'`
mem_kc_b=`echo $((0x$mem_kc_b))`
mem_kc_e=`cat /proc/iomem | grep code | awk '{print $1}' | awk -F "-" '{print $2}'`
mem_kc_e=`echo $((0x$mem_kc_e))`
mem_kd_b=`cat /proc/iomem | grep data | awk '{print $1}' | awk -F "-" '{print $1}'`
mem_kd_b=`echo $((0x$mem_kd_b))`
mem_kd_e=`cat /proc/iomem | grep data | awk '{print $1}' | awk -F "-" '{print $2}'`
mem_kd_e=`echo $((0x$mem_kd_e))`
let mem_kc=$mem_kc_e-$mem_kc_b
let mem_kd=$mem_kd_e-$mem_kd_b
let mem_k=$mem_kd+$mem_kc
let mem_u=`cat /proc/meminfo | grep ^MemTotal: | awk '{print $2}'`*1024
let mem_f=`cat /proc/meminfo | grep ^MemFree: | awk '{print $2}'`*1024
let mem_b=`cat /proc/meminfo | grep ^Buffers: | awk '{print $2}'`*1024
let mem_c=`cat /proc/meminfo | grep ^Cached: | awk '{print $2}'`*1024
let mem_s=`cat /proc/meminfo | grep ^SwapTotal: | awk '{print $2}'`*1024
let mem_s_f=`cat /proc/meminfo | grep ^SwapFree: | awk '{print $2}'`*1024
let mem_res=$mem_r-$mem_u
`rrdupdate "${MEM_D}" -t ram:usable:kernel_code:kernel_data:free:buff:cache:swap_free:swap_total N:"${mem_r}":"${mem_u}":"${mem_kc}":"${mem_kd}":"${mem_f}":"${mem_b}":"${mem_c}":"${mem_s_f}":"${mem_s}"`
# for Debud Memory Utilisation
let mem_delta=$mem_r-$mem_f-$mem_b-$mem_c-$mem_kc-$mem_kd-$mem_res
let mem_i=`cat /proc/meminfo | grep ^Inactive: | awk '{print $2}'`*1024
let mem_a=`cat /proc/meminfo | grep ^Active: | awk '{print $2}'`*1024
echo "----------------------------------------"
echo "System RAM --> $mem_r"
echo "Usable Memory --> $mem_u"
echo "Total Kernel --> $mem_k"
echo "Kernel code --> $mem_kc"
echo "Kernel data --> $mem_kd"
echo "Kernel reserved --> $mem_res"
echo "Free Memory --> $mem_f"
echo "Buffes --> $mem_b"
echo "Cached --> $mem_c"
echo "Swap total --> $mem_s"
echo "Swap free --> $mem_s_f"
echo "Delta mem --> $mem_delta"
echo "Inactive mem --> $mem_i"
echo "Active mem --> $mem_a"
echo "----------------------------------------"
if [ "$ip" = "" ] && [ `/bin/ls -l "$log_ip" | awk '{print $5}'` -ge 7 ]
then
get_ip
echo "ip=\"$ip\"" >> "$f_conf"
echo "ip_l=\"$ip_l\"" >> "$f_conf"
fi
#Create chain`s
a=`iptables-save | grep ":$c_t_in"`
if [ "$a" = '' ]
then
`iptables -N "$c_t_in"`
`iptables -I FORWARD "$MONIT" -o "$WLINT" -j "$c_t_in"`
fi
a=`iptables-save | grep ":$c_t_out"`
if [ "$a" = '' ]
then
`iptables -N "$c_t_out"`
`iptables -I FORWARD "$MONIT" -i "$WLINT" -j "$c_t_out"`
fi
i=2
for ip_v in $ip
do
a=`echo "$i ${ip_l}" | awk '{print $($1)}'`
if [ ! -f "${RRDDATA}/${a}.rrd" ]
then
CreateRRD "${RRDDATA}/${a}.rrd"
fi
b=`iptables-save | grep "A $c_t_in -d "$ip_v"`
if [ "$b" = '' ]
then
iptables -A "$c_t_in" -d "$ip_v"
iptables -A "$c_t_out" -s "$ip_v"
fi
b=`echo $ip_v | grep /`
if [ "$b" != "" ]
then
b=`echo $ip_v | sed 's/\// /'`
b=`ipcalc -p "$b"`
b=`echo ${b#*PREFIX=}`
ip_v=`echo $ip_v | sed "s/\/[0-9.]*/\/$b/"`
fi
c=`iptables -L "$c_t_in" -nvx | grep "$ip_v" | awk '{print $2}'`
#echo "iptables -L "$c_t_in" -nvx | grep "$ip_v" | awk '{print $2}'"
d=`iptables -L "$c_t_out" -nvx | grep "$ip_v" | awk '{print $2}'`
#echo "iptables -L "$c_t_out" -nvx | grep "$ip_v" | awk '{print $2}'"
`/opt/bin/rrdupdate "${RRDDATA}/${a}.rrd" -t in:out N:"$c":"$d"`
#echo "/opt/bin/rrdupdate "${RRDDATA}/${a}.rrd" -t in:out N:"$c":"$d""
i=$(($i+1))
done
if [ "$Monit_Ports" -eq 1 ]
then
if [ "$ports_l" = "" ] && [ -f "$log_ports" ] || [ ! -f "${RRDDATA}"/ports.rrd ]
then
if [ "$ports_l" = "" ]
then
ports_l=`cat "$log_ports"`
echo "ports_l=\"$ports_l\"" >> "$f_conf"
fi
i=1; a=""; b=""
c="rrdtool create ${RRDDATA}/ports.rrd"
for p_l in $ports_l
do
c="$c DS:tcp_p"$i"_in:DERIVE:600:0:104857600 \
DS:tcp_p"$i"_out:DERIVE:600:0:104857600 \
DS:udp_p"$i"_in:DERIVE:600:0:104857600 \
DS:udp_p"$i"_out:DERIVE:600:0:104857600"
i=$(($i+1))
done
c="$c DS:tcp_f_in:DERIVE:600:0:104857600 \
DS:tcp_f_out:DERIVE:600:0:104857600 \
DS:udp_f_in:DERIVE:600:0:104857600 \
DS:udp_f_out:DERIVE:600:0:104857600 \
DS:icmp_in:DERIVE:600:0:104857600 \
DS:icmp_out:DERIVE:600:0:104857600 \
DS:icmp_f_in:DERIVE:600:0:104857600 \
DS:icmp_f_out:DERIVE:600:0:104857600 \
RRA:AVERAGE:0.5:1:576 \
RRA:AVERAGE:0.5:12:336 \
RRA:AVERAGE:0.5:36:496"
`$c`
fi
a=`echo 2 $ports_l | awk '{print $($1)}'`
b=`iptables-save | grep "A $c_t_in -p tcp -m tcp --sport $a"`
if [ "$b" = "" ]
then
ports_rules_wl
fi
i=1; v_upd=""; p_upd=""
for p_l in $ports_l
do
a=`iptables -L "$c_t_in" -nvx | grep "tcp spt[s:]*$p_l " | awk '{print $2}'`
b=`iptables -L "$c_t_in" -nvx | grep "udp spt[s:]*$p_l " | awk '{print $2}'`
c=`iptables -L "$c_t_out" -nvx | grep "tcp dpt[s:]*$p_l " | awk '{print $2}'`
d=`iptables -L "$c_t_out" -nvx | grep "udp dpt[s:]*$p_l " | awk '{print $2}'`
v_upd="$v_upd":tcp_p"$i"_in:tcp_p"$i"_out:udp_p"$i"_in:udp_p"$i"_out
#echo "line 672 $v_upd"
p_upd="$p_upd":"$a":"$c":"$b":"$d"
i=$(($i+1))
done
a=`iptables -L "$c_t_in" -nvx | grep 'tcp -f' | awk '{print $2}'`
b=`iptables -L "$c_t_in" -nvx | grep 'udp -f' | awk '{print $2}'`
c=`iptables -L "$c_t_out" -nvx | grep 'tcp -f' | awk '{print $2}'`
d=`iptables -L "$c_t_out" -nvx | grep 'udp -f' | awk '{print $2}'`
e=`iptables -L "$c_t_in" -nvx | grep 'icmp [-\t 0-9./\*]* icmp type 255' | awk '{print $2}'`
f=`iptables -L "$c_t_out" -nvx | grep 'icmp [-\t 0-9./\*]* icmp type 255' | awk '{print $2}'`
g=`iptables -L "$c_t_in" -nvx | grep 'icmp -f' | awk '{print $2}'`
h=`iptables -L "$c_t_out" -nvx | grep 'icmp -f' | awk '{print $2}'`
v_upd=`echo $v_upd | sed 's/^://'`
#echo "line 685 $v_upd"
p_upd=`echo $p_upd | sed 's/^://'`
v_upd="$v_upd":tcp_f_in:tcp_f_out:udp_f_in:udp_f_out:icmp_in:icmp_out:icmp_f_in:icmp_f_out
#echo "line 688 $v_upd"
p_upd="$p_upd":"$a":"$c":"$b":"$d":"$e":"$f":"$g":"$h"
v_upd="/opt/bin/rrdupdate ${RRDDATA}/ports.rrd -t $v_upd N:$p_upd"
`$v_upd`
#echo "line 692 $v_upd"
fi
fi
# Section ===subroutines for graph create===
# $1 = ImageFile , $2 = Time in secs to go back , $3 = RRDfil , $4 = GraphText
CreateGraphCPU ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 220 -v "%" -r -l 0 -u 100 \
'DEF:d1='${3}':user:AVERAGE' \
'DEF:d2='${3}':system:AVERAGE' \
'DEF:d3='${3}':nice:AVERAGE' \
'DEF:d4='${3}':idle:AVERAGE' \
'CDEF:d_u=100,d1,d1,d2,d3,d4,+,+,+,/,*' \
'CDEF:d_s=100,d2,d1,d2,d3,d4,+,+,+,/,*' \
'CDEF:d_n=100,d3,d1,d2,d3,d4,+,+,+,/,*' \
'CDEF:d_i=100,d4,d1,d2,d3,d4,+,+,+,/,*' \
'CDEF:d_t=d_u,d_s,d_n,+,+' \
'CDEF:d_s_l=d_u,d_s,+' \
'VDEF:av=d_u,AVERAGE' \
'VDEF:av1=d_s,AVERAGE' \
'VDEF:av2=d_n,AVERAGE' \
'VDEF:av3=d_i,AVERAGE' \
'VDEF:av4=d_t,AVERAGE' \
'AREA:d_u#7fffd4:User ' \
GPRINT:d_u:MIN:"Min %6.2lf%% " \
GPRINT:d_u:MAX:"Max %6.2lf%% " \
GPRINT:av:"Aver %6.2lf%% " \
GPRINT:d_u:LAST:"Curr %6.2lf%% \n" \
'STACK:d_s#00bfff:System ' \
GPRINT:d_s:MIN:"Min %6.2lf%% " \
GPRINT:d_s:MAX:"Max %6.2lf%% " \
GPRINT:av1:"Aver %6.2lf%% " \
GPRINT:d_s:LAST:"Curr %6.2lf%% \n" \
'STACK:d_n#ffff00:Nice ' \
GPRINT:d_n:MIN:"Min %6.2lf%% " \
GPRINT:d_n:MAX:"Max %6.2lf%% " \
GPRINT:av2:"Aver %6.2lf%% " \
GPRINT:d_n:LAST:"Curr %6.2lf%% \n" \
'STACK:d_i#ffffff:Idle ' \
GPRINT:d_i:MIN:"Min %6.2lf%% " \
GPRINT:d_i:MAX:"Max %6.2lf%% " \
GPRINT:av3:"Aver %6.2lf%% " \
GPRINT:d_i:LAST:"Curr %6.2lf%% \n" \
'LINE1:d_t#ff0000:Total utilization ' \
GPRINT:d_t:MIN:"Min %6.2lf%% " \
GPRINT:d_t:MAX:"Max %6.2lf%% " \
GPRINT:av4:"Aver %6.2lf%% " \
GPRINT:d_t:LAST:"Curr %6.2lf%% \n" \
'LINE1:d_u#458b74'\
'LINE1:d_s_l#00688b'\
'LINE1:d_t#ff0000' \
-t "${4}"
mv -f "${1}.new" "${1}"
}
# $1 = ImageFile , $2 = Time in secs to go back , $3 = RRDfil , $4 = GraphText
CreateGraphMEM ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 200 -v "bytes" -b 1024 -r -l 0 \
'DEF:d1='${3}':ram:AVERAGE' \
'DEF:d2='${3}':usable:AVERAGE' \
'DEF:d3='${3}':kernel_code:AVERAGE' \
'DEF:d4='${3}':kernel_data:AVERAGE' \
'DEF:d5='${3}':free:AVERAGE' \
'DEF:d6='${3}':buff:AVERAGE' \
'DEF:d7='${3}':cache:AVERAGE' \
'DEF:d8='${3}':swap_free:AVERAGE' \
'DEF:d9='${3}':swap_total:AVERAGE' \
'CDEF:d10=d1,d2,d3,d4,+,+,-' \
'CDEF:d11=d9,d8,-' \
'CDEF:d12=d1,d3,d4,d10,d5,d6,d7,+,+,+,+,+,-' \
'CDEF:l_kern=d3,d4,d10,+,+' \
'CDEF:l_buf=d3,d4,d10,d6,+,+,+' \
'CDEF:l_cache=l_buf,d7,+' \
'CDEF:l_app=l_cache,d12,+' \
'CDEF:l_free=l_app,d5,+' \
'CDEF:l_swap=l_free,d11,+' \
'VDEF:av3=d3,AVERAGE' \
'VDEF:av4=d4,AVERAGE' \
'VDEF:av5=d5,AVERAGE' \
'VDEF:av6=d6,AVERAGE' \
'VDEF:av7=d7,AVERAGE' \
'VDEF:av10=d10,AVERAGE' \
'VDEF:av11=d11,AVERAGE' \
'VDEF:av12=d12,AVERAGE' \
'AREA:d3#8db6cd:Kernel code ' \
GPRINT:d3:MIN:"Min %6.2lf%s " \
GPRINT:d3:MAX:"Max %6.2lf%s " \
GPRINT:av3:"Aver %6.2lf%s " \
GPRINT:d3:LAST:"Cur %6.2lf%s \n" \
'STACK:d4#a4d3ee:Kernel data ' \
GPRINT:d4:MIN:"Min %6.2lf%s " \
GPRINT:d4:MAX:"Max %6.2lf%s " \
GPRINT:av4:"Aver %6.2lf%s " \
GPRINT:d4:LAST:"Cur %6.2lf%s \n" \
'STACK:d10#b0e2ff:Kernel reserved ' \
GPRINT:d10:MIN:"Min %6.2lf%s " \
GPRINT:d10:MAX:"Max %6.2lf%s " \
GPRINT:av10:"Aver %6.2lf%s " \
GPRINT:d10:LAST:"Cur %6.2lf%s \n" \
'STACK:d6#ffc125:Buffer ' \
GPRINT:d6:MIN:"Min %6.2lf%s " \
GPRINT:d6:MAX:"Max %6.2lf%s " \
GPRINT:av6:"Aver %6.2lf%s " \
GPRINT:d6:LAST:"Cur %6.2lf%s \n" \
'STACK:d7#00b2ee:Cache ' \
GPRINT:d7:MIN:"Min %6.2lf%s " \
GPRINT:d7:MAX:"Max %6.2lf%s " \
GPRINT:av7:"Aver %6.2lf%s " \
GPRINT:d7:LAST:"Cur %6.2lf%s \n" \
'STACK:d12#ffff00:Applications ' \
GPRINT:d12:MIN:"Min %6.2lf%s " \
GPRINT:d12:MAX:"Max %6.2lf%s " \
GPRINT:av12:"Aver %6.2lf%s " \
GPRINT:d12:LAST:"Cur %6.2lf%s \n" \
'STACK:d5#54ff9f:Free ' \
GPRINT:d5:MIN:"Min %6.2lf%s " \
GPRINT:d5:MAX:"Max %6.2lf%s " \
GPRINT:av5:"Aver %6.2lf%s " \
GPRINT:d5:LAST:"Cur %6.2lf%s \n" \
'STACK:d11#ff0000:Swap used ' \
GPRINT:d11:MIN:"Min %6.2lf%s " \
GPRINT:d11:MAX:"Max %6.2lf%s " \
GPRINT:av11:"Aver %6.2lf%s " \
GPRINT:d11:LAST:"Cur %6.2lf%s \n" \
'LINE1:l_buf#8b6914' \
'LINE1:l_cache#00688b' \
'LINE1:l_app#8b8b00' \
'LINE1:l_free#2e8b57' \
-t "${4}"
mv -f "${1}.new" "${1}"
}
# $1 = ImageFile , $2 = Time in secs to go back , $3 = RRDfil , $4 = GraphText
CreateGraph ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 150 -v "bytes/s" -l 0 --font DEFAULT:7 \
'DEF:in='${3}':in:AVERAGE' \
'DEF:out='${3}':out:AVERAGE' \
'VDEF:sum_in=in,TOTAL' \
'VDEF:sum_out=out,TOTAL' \
'CDEF:overlap=in,out,LT,in,out,IF' \
'AREA:in#ffc125:In ' \
GPRINT:in:MIN:"Min %6.2lf%s " \
GPRINT:in:MAX:"Max %6.2lf%s " \
GPRINT:in:AVERAGE:"Avg %6.2lf%s " \
GPRINT:in:LAST:"Curr %6.2lf%s " \
GPRINT:sum_in:"Total %6.2lf%s \n" \
'AREA:out#54ff9f:Out' \
GPRINT:out:MIN:"Min %6.2lf%s " \
GPRINT:out:MAX:"Max %6.2lf%s " \
GPRINT:out:AVERAGE:"Avg %6.2lf%s " \
GPRINT:out:LAST:"Curr %6.2lf%s " \
GPRINT:sum_out:"Total %6.2lf%s \n" \
'AREA:overlap#cdcd00'\
'LINE1:in#cd9b1d'\
'LINE1:out#009900'\
-t "${4}"
mv -f "${1}.new" "${1}"
}
# $1 = ImageFile , $2 = Time in secs to go back , $3 = RRDfil , $4 = GraphText
CreateGraphIP ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 100 -v "bytes/s" -l 0 --font DEFAULT:7 \
'DEF:in='${3}':in:AVERAGE' \
'DEF:out='${3}':out:AVERAGE' \
'VDEF:sum_in=in,TOTAL' \
'VDEF:sum_out=out,TOTAL' \
'CDEF:overlap=in,out,LT,in,out,IF' \
'AREA:in#ffc125:In ' \
GPRINT:in:MIN:"Min %6.2lf%s " \
GPRINT:in:MAX:"Max %6.2lf%s " \
GPRINT:in:AVERAGE:"Avg %6.2lf%s " \
GPRINT:in:LAST:"Cur %6.2lf%s " \
GPRINT:sum_in:"Total %6.2lf%s \n" \
'AREA:out#54ff9f:Out' \
GPRINT:out:MIN:"Min %6.2lf%s " \
GPRINT:out:MAX:"Max %6.2lf%s " \
GPRINT:out:AVERAGE:"Avg %6.2lf%s " \
GPRINT:out:LAST:"Cur %6.2lf%s " \
GPRINT:sum_out:"Total %6.2lf%s \n" \
'AREA:overlap#cdcd00'\
'LINE1:in#cd9b1d' \
'LINE1:out#009900' \
-t "${4}"
mv -f "${1}.new" "${1}"
}
# $1 = ImageFile , $2 = Time in secs to go back , $3 = GraphText , $4 = Port number
CreateGraphPorts ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 220 -v "bytes/s" --font DEFAULT:7 \
'DEF:t_in_t='${RRDDATA}/ports.rrd':tcp_p'"$4"'_in:AVERAGE' \
'DEF:t_out_t='${RRDDATA}/ports.rrd':tcp_p'"$4"'_out:AVERAGE' \
'DEF:u_in='${RRDDATA}/ports.rrd':udp_p'"$4"'_in:AVERAGE' \
'DEF:u_out='${RRDDATA}/ports.rrd':udp_p'"$4"'_out:AVERAGE' \
'CDEF:t_in=0,t_in_t,-' \
'CDEF:t_out=0,t_out_t,-' \
'CDEF:overlap_t=t_out,t_in,LT,t_in,t_out,IF' \
'CDEF:overlap_u=u_in,u_out,LT,u_in,u_out,IF' \
'VDEF:sum_t_in_t=t_in_t,TOTAL' \
'VDEF:sum_t_out_t=t_out_t,TOTAL' \
'VDEF:sum_u_in=u_in,TOTAL' \
'VDEF:sum_u_out=u_out,TOTAL' \
COMMENT:"UDP Graph \n" \
'AREA:u_in#ffa500:In ' \
GPRINT:u_in:MIN:"Min %6.2lf%s " \
GPRINT:u_in:MAX:"Max %6.2lf%s " \
GPRINT:u_in:AVERAGE:"Avg %6.2lf%s " \
GPRINT:u_in:LAST:"Cur %6.2lf%s " \
GPRINT:sum_u_in:"Total %6.2lf%s \n" \
'AREA:u_out#c0ff3e:Out ' \
GPRINT:u_out:MIN:"Min %6.2lf%s " \
GPRINT:u_out:MAX:"Max %6.2lf%s " \
GPRINT:u_out:AVERAGE:"Avg %6.2lf%s " \
GPRINT:u_out:LAST:"Cur %6.2lf%s " \
GPRINT:sum_u_out:"Total %6.2lf%s \n" \
COMMENT:"---------------------------------------------------------------------------------------------------------------------------------------\n" \
COMMENT:"TCP Graph \n" \
'AREA:t_in#ff8c69:In ' \
GPRINT:t_in_t:MIN:"Min %6.2lf%s " \
GPRINT:t_in_t:MAX:"Max %6.2lf%s " \
GPRINT:t_in_t:AVERAGE:"Avg %6.2lf%s " \
GPRINT:t_in_t:LAST:"Cur %6.2lf%s " \
GPRINT:sum_t_in_t:"Total %6.2lf%s \n" \
'AREA:t_out#9aff9a:Out ' \
GPRINT:t_out_t:MIN:"Min %6.2lf%s " \
GPRINT:t_out_t:MAX:"Max %6.2lf%s " \
GPRINT:t_out_t:AVERAGE:"Avg %6.2lf%s " \
GPRINT:t_out_t:LAST:"Cur %6.2lf%s " \
GPRINT:sum_t_out_t:"Total %6.2lf%s \n" \
'AREA:overlap_t#cdcd00' \
'AREA:overlap_u#eeee00' \
'LINE1:t_in#cd7054'\
'LINE1:t_out#548b54'\
'LINE1:u_in#cd8500'\
'LINE1:u_out#698b22'\
-t "${3}"
mv -f "${1}.new" "${1}"
}
CreateGraphICMP ()
{
/opt/bin/rrdtool graph "${1}.new" -a PNG -R light -s -"${2}" -w $graph_w -h 220 -v "bytes/s" -l 0 --font DEFAULT:7 \
'DEF:icmp_in='${RRDDATA}/ports.rrd':icmp_in:AVERAGE' \
'DEF:icmp_out='${RRDDATA}/ports.rrd':icmp_out:AVERAGE' \
'CDEF:overlap_icmp=icmp_in,icmp_out,LT,icmp_in,icmp_out,IF' \
'VDEF:sum_icmp_in=icmp_in,TOTAL' \
'VDEF:sum_icmp_out=icmp_out,TOTAL' \
'AREA:icmp_in#ffaeb9:In ' \
GPRINT:icmp_in:MAX:"Max %6.2lf%s " \
GPRINT:icmp_in:MIN:"Min %6.2lf%s " \
GPRINT:icmp_in:AVERAGE:"Avg %6.2lf%s " \
GPRINT:icmp_in:LAST:"Cur %6.2lf%s " \
GPRINT:sum_icmp_in:"Total %6.2lf%s \n" \
'AREA:icmp_out#caff70:Out ' \
GPRINT:icmp_out:MAX:"Max %6.2lf%s " \
GPRINT:icmp_out:MIN:"Min %6.2lf%s " \
GPRINT:icmp_out:AVERAGE:"Avg %6.2lf%s " \
GPRINT:icmp_out:LAST:"Cur %6.2lf%s " \
GPRINT:sum_icmp_out:"Total %6.2lf%s \n" \
'AREA:overlap_icmp#eee0e5'\
'LINE1:icmp_in#ee6aa7'\
'LINE1:icmp_out#6e8b3d'\
-t "${3}"
mv -f "${1}.new" "${1}"
}
# Section ===Update graphs===
# Set time-variables
MTIME=`date "+%M"`
HTIME=`date "+%H"`
DTIME=`date "+%u"`
dmTIME=`date "+%e"`
dyTIME=`date "+%j"`
stime="generated - `date`"
# Update graph CPU and Memory
# Daily
if [ "{$1}" = "{$up_cpu}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Daily $c_u and $mem_u created...
"
st=`echo "$HTIME $MTIME" | awk '{print ($1*60+$2)*60}'` #$((($HTIME*60+$MTIME)*60)) max st = 172800 sec = 48 hour
CreateGraphCPU "${RRDIMG}/cpu.png" "$st" "${CPU_D}" "CPU Utilization $stime"
CreateGraphMEM "${RRDIMG}/mem.png" "$st" "${MEM_D}" "Memory Utilization $stime"
echo "
Done.
"
fi
# Last 24 hours
if [ "{$1}" = "{$up_cpu_24}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Last 24 hours $c_u and $mem_u created...
"
st=86400
CreateGraphCPU "${RRDIMG}/cpu24.png" "$st" "${CPU_D}" "CPU Utilization $stime"
CreateGraphMEM "${RRDIMG}/mem24.png" "$st" "${MEM_D}" "Memory Utilization $stime"
echo "
Done.
"
fi
# Last 48 hours
if [ "{$1}" = "{$up_cpu_48}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Last 48 hours $c_u and $mem_u created...
"
st=172800
CreateGraphCPU "${RRDIMG}/cpu48.png" "$st" "${CPU_D}" "CPU Utilization $stime"
CreateGraphMEM "${RRDIMG}/mem48.png" "$st" "${MEM_D}" "Memory Utilization $stime"
echo "
Done.
"
fi
# Update graph WAN, LAN, WLAN
# Daily
if [ "$1" = "$up_s_g_d" ] || [ "$1" = "$up_g" ]
then
echo "
Creating Interfaces $D_G...
"
st=`echo "$HTIME $MTIME" | awk '{print ($1*60+$2)*60}'` #$((($HTIME*60+$MTIME)*60))
CreateGraph "${RRDIMG}/wanday.png" "$st" "${WANRRD}" "WAN $stime"
CreateGraph "${RRDIMG}/landay.png" "$st" "${LANRRD}" "LAN $stime"
CreateGraph "${RRDIMG}/wlanday.png" "$st" "${WLANRRD}" "WLAN $stime"
echo "
Done.
"
fi
# Weekly
if [ "{$1}" = "{$up_s_g_w}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Interfaces $W_G...
"
st=`echo "$HTIME $MTIME $DTIME" | awk '{print $3*24*3600+($1*60+$2)*60}'`
CreateGraph "${RRDIMG}/wanweek.png" "$st" "${WANRRD}" "WAN $stime"
CreateGraph "${RRDIMG}/lanweek.png" "$st" "${LANRRD}" "LAN $stime"
CreateGraph "${RRDIMG}/wlanweek.png" "$st" "${WLANRRD}" "WLAN $stime"
echo "
Done.
"
fi
# Monthly
if [ "{$1}" = "{$up_s_g_m}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Interfaces $M_G...
"
st=`echo "$HTIME $MTIME $dmTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
CreateGraph "${RRDIMG}/wanmonth.png" "$st" "${WANRRD}" "WAN $stime"
CreateGraph "${RRDIMG}/lanmonth.png" "$st" "${LANRRD}" "LAN $stime"
CreateGraph "${RRDIMG}/wlanmonth.png" "$st" "${WLANRRD}" "WLAN $stime"
echo "
Done.
"
fi
# Yearly
if [ "{$1}" = "{$up_s_g_y}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Interfaces $Y_G...
"
st=`echo "$HTIME $MTIME $dyTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
CreateGraph "${RRDIMG}/wanyear.png" "$st" "${WANRRD}" "WAN $stime"
CreateGraph "${RRDIMG}/lanyear.png" "$st" "${LANRRD}" "LAN $stime"
CreateGraph "${RRDIMG}/wlanyear.png" "$st" "${WLANRRD}" "WLAN $stime"
echo "
Done.
"
fi
# Update graph IP
# Daily
if [ "{$1}" = "{$up_s_ip_d}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating IP $D_G...
"
get_ip
i=2
for ip_v in $ip_l
do
a=`echo "$i ${ip}" | awk '{print $($1)}' #| sed 's/\// /'`
st=`echo "$HTIME $MTIME" | awk '{print ($1*60+$2)*60}'`
CreateGraphIP "${RRDIMG}/${ip_v}_d.png" "$st" "${RRDDATA}/${ip_v}.rrd" "$D_G $a, $stime"
i=$(($i+1))
done
echo "
Done.
"
fi
# Weekly
if [ "{$1}" = "{$up_s_ip_w}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating IP $W_G...
"
get_ip
i=2
for ip_v in $ip_l
do
a=`echo "$i ${ip}" | awk '{print $($1)}' #| sed 's/\// /'`
st=`echo "$HTIME $MTIME $DTIME" | awk '{print $3*24*3600+($1*60+$2)*60}'`
CreateGraphIP "${RRDIMG}/${ip_v}_w.png" "$st" "${RRDDATA}/${ip_v}.rrd" "$W_G $a, $stime"
i=$(($i+1))
done
echo "
Done.
"
fi
# Monthly
if [ "{$1}" = "{$up_s_ip_m}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating IP $M_G...
"
get_ip
i=2
for ip_v in $ip_l
do
a=`echo "$i ${ip}" | awk '{print $($1)}' #| sed 's/\// /'`
st=`echo "$HTIME $MTIME $dmTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
CreateGraphIP "${RRDIMG}/${ip_v}_m.png" "$st" "${RRDDATA}/${ip_v}.rrd" "$M_G $a, $stime"
i=$(($i+1))
done
echo "
Done.
"
fi
# Yearly
if [ "{$1}" = "{$up_s_ip_y}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating IP $Y_G...
"
get_ip
i=2
for ip_v in $ip_l
do
a=`echo "$i ${ip}" | awk '{print $($1)}' #| sed 's/\// /'`
st=`echo "$HTIME $MTIME $dyTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
CreateGraphIP "${RRDIMG}/${ip_v}_y.png" "$st" "${RRDDATA}/${ip_v}.rrd" "$Y_G $a, $stime"
i=$(($i+1))
done
echo "
Done.
"
fi
# Update graph port's
# Daily
if [ "{$1}" = "{$up_s_p_d}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Port's $D_G...
"
st=`echo "$HTIME $MTIME" | awk '{print ($1*60+$2)*60}'`
i=1
for v_p in $ports_l
do
v_f=`echo $v_p | sed 's/:/_/'`
CreateGraphPorts "${RRDIMG}/${v_f}_p_d.png" "$st" "$D_G port $v_p, $stime" "$i"
i=$(($i+1))
done
echo "
Done.
"
echo "
Creating ICMP $D_G...
"
CreateGraphICMP "${RRDIMG}/icmp_d.png" "$st" "$D_G ICMP, $stime"
echo "
Done.
"
fi
# Weekly
if [ "{$1}" = "{$up_s_p_w}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Port's $W_G...
"
st=`echo "$HTIME $MTIME $DTIME" | awk '{print $3*24*3600+($1*60+$2)*60}'`
i=1
for v_p in $ports_l
do
v_f=`echo $v_p | sed 's/:/_/'`
CreateGraphPorts "${RRDIMG}/${v_f}_p_w.png" "$st" "$W_G port $v_p, $stime" "$i"
i=$(($i+1))
done
echo "
Done.
"
echo "
Creating ICMP $W_G...
"
CreateGraphICMP "${RRDIMG}/icmp_w.png" "$st" "$W_G ICMP, $stime"
echo "
Done.
"
fi
# Monthly
if [ "{$1}" = "{$up_s_p_m}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Port's $M_G...
"
st=`echo "$HTIME $MTIME $dmTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
i=1
for v_p in $ports_l
do
v_f=`echo $v_p | sed 's/:/_/'`
CreateGraphPorts "${RRDIMG}/${v_f}_p_m.png" "$st" "$M_G port $v_p, $stime" "$i"
i=$(($i+1))
done
echo "
Done.
"
echo "
Creating ICMP $M_G...
"
CreateGraphICMP "${RRDIMG}/icmp_m.png" "$st" "$M_G ICMP, $stime"
echo "
Done.
"
fi
# Yearly
if [ "{$1}" = "{$up_s_p_y}" ] || [ "{$1}" = "{$up_g}" ]
then
echo "
Creating Port's $Y_G...
"
st=`echo "$HTIME $MTIME $dyTIME" | awk '{print ($3-1)*24*3600+($1*60+$2)*60}'`
i=1
for v_p in $ports_l
do
v_f=`echo $v_p | sed 's/:/_/'`
CreateGraphPorts "${RRDIMG}/${v_f}_p_y.png" "$st" "$Y_G port $v_p, $stime" "$i"
i=$(($i+1))
done
echo "
Done.
"
echo "
Creating ICMP $Y_G...
"
CreateGraphICMP "${RRDIMG}/icmp_y.png" "$st" "$Y_G ICMP, $stime"
echo "
Done.
"
fi
echo "
============= Script Done =============
"
echo ""