first commit
This commit is contained in:
11
scripts/Monitor.service
Normal file
11
scripts/Monitor.service
Normal file
@@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=Monitor
|
||||
|
||||
[Service]
|
||||
ExecStart=/opt/scripts/Monitor.sh
|
||||
TimeoutStopSec=8
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
25
scripts/Monitor.sh
Executable file
25
scripts/Monitor.sh
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
INTERVAL=2
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
type sar > /dev/null || exit 1
|
||||
}
|
||||
|
||||
function Main {
|
||||
cd $(dirname $0) || exit 1
|
||||
while sleep $INTERVAL; do
|
||||
for proc in $(find . -type f -name "mon_*" \
|
||||
-executable); do
|
||||
$proc &
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
1
scripts/conn.list
Normal file
1
scripts/conn.list
Normal file
@@ -0,0 +1 @@
|
||||
localhost:5000
|
51
scripts/mon_conn
Executable file
51
scripts/mon_conn
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
ADDR_FILE="$(dirname $0)/conn.list"
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="conn"
|
||||
INTERVAL=60
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function CountAddrConn {
|
||||
local addr=$1
|
||||
local server="${addr%:*}"
|
||||
local port="${addr#*:}"
|
||||
local count=0
|
||||
if [ 'localhost' = "$server" ]; then
|
||||
count=$(ss -anpt | awk '{print $4}' \
|
||||
| grep -c ":$port$")
|
||||
else
|
||||
count=$(ss -anpt | awk '{print $5}' \
|
||||
| grep -c "$addr$")
|
||||
fi
|
||||
echo "$server $port $count"
|
||||
}
|
||||
|
||||
function Main {
|
||||
local addr=
|
||||
sleep $INTERVAL
|
||||
for addr in $(cat $ADDR_FILE); do
|
||||
Log "$(CountAddrConn $addr)"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
38
scripts/mon_cpu
Executable file
38
scripts/mon_cpu
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="cpu"
|
||||
INTERVAL=60
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
type sar > /dev/null || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetCPULoad {
|
||||
top -b -n 1 | sed -n '3p' | sed 's/^.*://' | tr , ' ' \
|
||||
| awk '{print $1,$3,$9,$7}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
sleep $INTERVAL
|
||||
Log "$(GetCPULoad)"
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
39
scripts/mon_disk
Executable file
39
scripts/mon_disk
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="disk"
|
||||
INTERVAL=300
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetDiskInfo {
|
||||
df | grep '^/dev/' \
|
||||
| awk '{print $1,substr($5,0,length($5)-1)}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
sleep $INTERVAL
|
||||
GetDiskInfo|while read line; do
|
||||
Log "$line"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
42
scripts/mon_io
Executable file
42
scripts/mon_io
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="io"
|
||||
SAR_INTERVAL=20
|
||||
SAR_COUNT=6
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
type sar > /dev/null || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetIOInfo {
|
||||
sar -dp $SAR_INTERVAL $SAR_COUNT | grep '^Average' \
|
||||
| tail -n +2 \
|
||||
| awk '{print $2,$3,-$4/2,$5/2,$8,$10}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
local line=
|
||||
GetIOInfo | while read line; do
|
||||
Log "$line"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
51
scripts/mon_kafka
Executable file
51
scripts/mon_kafka
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
export JAVA_HOME=/opt/jre
|
||||
export PATH=$JAVA_HOME/bin:$PATH
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="kafka"
|
||||
INTERVAL=60
|
||||
KAFKA_ROOT="/opt/kafka"
|
||||
KAFKA_SERVERS="10.0.4.104:9092,10.0.4.105:9092,10.0.4.106:9092"
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetKafkaInfo {
|
||||
local consumer_group=
|
||||
cd $KAFKA_ROOT/bin || return 1
|
||||
for consumer_group in $(./kafka-consumer-groups.sh \
|
||||
--bootstrap-server $KAFKA_SERVERS --list); do
|
||||
./kafka-consumer-groups.sh \
|
||||
--bootstrap-server $KAFKA_SERVERS \
|
||||
--group $consumer_group --describe \
|
||||
| tail -n +3 | awk '$7 !~ /^-$/{print $1,$2,
|
||||
$3,$4,$5,substr($7,2),"'$consumer_group'"}'
|
||||
done
|
||||
}
|
||||
|
||||
function Main {
|
||||
sleep $INTERVAL
|
||||
GetKafkaInfo|while read line; do
|
||||
Log "$line"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
38
scripts/mon_mem
Executable file
38
scripts/mon_mem
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="mem"
|
||||
INTERVAL=60
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetMEMInfo {
|
||||
free -w | grep '^Mem' \
|
||||
| awk '{printf "%.2f %.2f %.2f %.2f\n",$3*100/$2,
|
||||
$4*100/$2,$6*100/$2,$7*100/$2}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
sleep $INTERVAL
|
||||
Log "$(GetMEMInfo)"
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
41
scripts/mon_net
Executable file
41
scripts/mon_net
Executable file
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="net"
|
||||
SAR_INTERVAL=50
|
||||
SAR_COUNT=6
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
type sar > /dev/null || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetNetInfo {
|
||||
sar -n DEV $SAR_INTERVAL $SAR_COUNT | grep '^Average' \
|
||||
| tail -n +2 | awk '{print $2,$5,-$6}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
local line=
|
||||
GetNetInfo | while read line; do
|
||||
Log "$line"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
42
scripts/mon_ping
Executable file
42
scripts/mon_ping
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
PING_FILE="$(dirname $0)/ping.list"
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="ping"
|
||||
INTERVAL=120
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
type fping > /dev/null || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetPingInfo {
|
||||
fping -A -f $PING_FILE \
|
||||
| awk '{print $1,$3,"alive"==$3?1:0}'
|
||||
}
|
||||
|
||||
function Main {
|
||||
local line=
|
||||
sleep $INTERVAL
|
||||
GetPingInfo|while read line; do
|
||||
Log "$line"
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
55
scripts/mon_proc
Executable file
55
scripts/mon_proc
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
export LANG=en_US.UTF-8
|
||||
PROC_FILE="$(dirname $0)/proc.list"
|
||||
LOG_PATH="/var/log/monitor"
|
||||
LOG_NAME="proc"
|
||||
INTERVAL=60
|
||||
|
||||
function Init {
|
||||
local self_count=$(pgrep -cx "$(basename $0)")
|
||||
[ 0 -eq $? ] || exit 1
|
||||
[ 1 -eq $self_count ] || exit 1
|
||||
mkdir -p $LOG_PATH || exit 1
|
||||
}
|
||||
|
||||
function Log {
|
||||
local msg="$1"
|
||||
local log_time="$(date +'%F %T')"
|
||||
local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
|
||||
echo "$log_time $msg" >> $log_file
|
||||
cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
|
||||
| head -n -7 | xargs rm -f
|
||||
}
|
||||
|
||||
function GetProcInfo {
|
||||
local line="$1"
|
||||
local class="${line%% *}"
|
||||
local proc="${line#* }"
|
||||
local stat_code=1
|
||||
local stat=
|
||||
if [ 'service' = "$class" ]; then
|
||||
stat="$(systemctl status $proc \
|
||||
| grep -m 1 '^ Active:' \
|
||||
| awk '{print $2}')"
|
||||
[ 'active' = "$stat" ] && stat_code=0
|
||||
[ -z "$stat" ] && stat="unknown"
|
||||
else
|
||||
pgrep -f "$proc" &> /dev/null && stat_code=0 \
|
||||
&& stat='running' || stat='stopped'
|
||||
fi
|
||||
echo "$class#$proc#$stat#$stat_code"
|
||||
}
|
||||
|
||||
function Main {
|
||||
local line=
|
||||
sleep $INTERVAL
|
||||
while read line; do
|
||||
Log "$(GetProcInfo "$line")"
|
||||
done < $PROC_FILE
|
||||
}
|
||||
|
||||
# start
|
||||
Init
|
||||
Main
|
||||
|
2
scripts/ping.list
Normal file
2
scripts/ping.list
Normal file
@@ -0,0 +1,2 @@
|
||||
192.168.1.1
|
||||
192.168.1.2
|
2
scripts/proc.list
Normal file
2
scripts/proc.list
Normal file
@@ -0,0 +1,2 @@
|
||||
service EmotionJudger
|
||||
daemon java .*-jar .*\\<filename.jar
|
Reference in New Issue
Block a user