CPU、内存使用率:
top -n 1 -b | grep -E '^[[:alpha:]]' > $(date+%Y-%m-%d-%H)_cpu_mem.txt
进程占用内存(降序):
top -n 1 -b | grep -v -E '^[[:alpha:]]|^$|COMMAND' | awk '{print $9 "\t" $10"\t" $11 "\t\t" $12}' | sort -k2nr > $(date+%Y-%m-%d-%H)_process_mem.txt
进程占用CPU率(降序):
top -n 1 -b | grep -v -E '^[[:alpha:]]|^$|COMMAND' | awk '{print $9 "\t" $10"\t" $11 "\t\t" $12}' | sort -k1nr > $(date+%Y-%m-%d-%H)_process_cpu.txt
进程数量:
ps -ef> $(date +%Y-%m-%d-%H)_process.txt
磁盘使用率:
df -Th> $(date +%Y-%m-%d-%H)_disk.txt
监控脚本文件: install_monitor.sh
#!/bin/sh
basefile=$(cd "$( dirname "$0")" && pwd)
chmod +x $(basefile)/print_top.sh
echo ${basefile}
echo "*****************settings crontab start*****************"
echo "*/1 * * * * . /etc/profile;/bin/sh" ${basefile}"/print_top.sh" >>/var/spool/cron/root
echo "*****************settings crontab end *****************"
echo " top monitor is running ,please show print_top.txt at /opt/opzoonface/logs "
systemctl restart crond.service
监控脚本文件: print_top.sh
echo $(date +%Y-%m-%d\ %T) ':' >> print_cpu_mem.txt
top -n 1 -b | grep -E '^[[:alpha:]]' >> print_cpu_mem.txt
echo $(date +%Y-%m-%d\ %T) ':' >> print_process_mem.txt
top -n 1 -b | grep -v -E '^[[:alpha:]]|^$|COMMAND' | awk '{print $9 "\t" $10"\t" $11 "\t\t" $12}' | sort -k2nr >> print_process_mem.txt
echo $(date +%Y-%m-%d\ %T) ':' >> print_process_cpu.txt
top -n 1 -b | grep -v -E '^[[:alpha:]]|^$|COMMAND' | awk '{print $9 "\t" $10"\t" $11 "\t\t" $12}' | sort -k1nr >> print_process_cpu.txt
简洁版print_top.sh
#!/bin/sh
echo $(date +%Y-%m-%d\ %T) ':' >> /opt/opzoonface/logs/print_top.txt
#top -n 1 -b | grep -E '^[[:alpha:]]' >> print_cpu_mem.txt
#echo $(date +%Y-%m-%d\ %T) ':' >> print_process_mem.txt
top -n 1 -b | head -n 20 >> /opt/opzoonface/logs/print_top.txt
#echo $(date +%Y-%m-%d\ %T) ':' >> print_process_cpu.txt
#top -n 1 -b | grep -v -E '^[[:alpha:]]|^$|COMMAND' | awk '{print $9 "\t" $10"\t" $11 "\t\t" $12}' | sort -k1nr >> print_process_cpu.txt
~~~输出: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2018-03-07 17:13:01 :
top - 17:13:01 up 7 days, 23:49, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 132 total, 1 running, 129 sleeping, 0 stopped, 2 zombie
%Cpu(s): 0.5 us, 0.2 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 20397000 total, 15010072 free, 4761636 used, 625292 buff/cache
KiB Swap: 5242876 total, 5242876 free, 0 used. 15305304 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
731 root 20 0 437304 7524 5956 S 6.7 0.0 0:09.16 NetworkMan+
20715 root 20 0 129888 1596 1200 R 6.7 0.0 0:00.01 top
1 root 20 0 141724 7960 2712 S 0.0 0.0 0:37.17 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.83 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 1:14.47 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+
7 root rt 0 0 0 0 S 0.0 0.0 0:35.79 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/1
11 root 20 0 0 0 0 S 0.0 0.0 6:04.03 rcu_sched
12 root 20 0 0 0 0 S 0.0 0.0 5:21.78 rcuos/0
13 root 20 0 0 0 0 S 0.0 0.0 5:47.83 rcuos/1
##################################################
常用io命令: iotop iostat -x 1