如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息相关的知识,希望对你有一定的参考价值。
1,Linux下可以在/proc/cpuinfo中看到每个cpu的详细信息。但是对于双核的cpu,在cpuinfo中会看到两个cpu。常常会让人误以为是两个单核的cpu。其实应该通过Physical
Processor
ID来区分单核和双核。而Physical
Processor
ID可以从cpuinfo或者dmesg中找到.
flags
如果有
ht
说明
支持超线程技术
判断物理CPU的个数可以查看physical
id
的值,相同则为同一个物理CPU
2,查看内存大小:
cat
/proc/meminfo
|grep
MemTotal
3,其他一些可以查看详细
linux系统
信息的命令和方法:
uname
-a
#
查看内核/操作系统/
CPU信息
的linux系统信息命令
head
-n
1
/etc/issue
#
查看操作系统版本,是数字1不是字母L
cat
/proc/cpuinfo
#
查看CPU信息的linux系统信息命令
hostname
#
查看计算机名的linux系统信息命令
lspci
-tv
#
列出所有
PCI设备
lsusb
-tv
#
列出所有USB设备的linux系统信息命令
lsmod
#
列出加载的内核模块
env
#
查看
环境变量
资源
free
-m
#
查看内存使用量和
交换区
使用量
df
-h
#
查看各分区使用情况
du
-sh
#
查看指定目录的大小
grep
MemTotal
/proc/meminfo
#
查看内存总量
grep
MemFree
/proc/meminfo
#
查看空闲内存量
uptime
#
查看系统
运行时间
、用户数、负载
cat
/proc/loadavg
#
查看系统负载磁盘和分区
mount
|
column
-t
#
查看挂接的分区状态
fdisk
-l
#
查看所有分区
swapon
-s
#
查看所有
交换分区
hdparm
-i
/dev/hda
#
查看磁盘参数(仅适用于
IDE设备
)
dmesg
|
grep
IDE
#
查看启动时IDE设备检测状况网络
ifconfig
#
查看所有网络接口的属性
iptables
-L
#
查看防火墙设置
route
-n
#
查看
路由表
netstat
-lntp
#
查看所有监听端口
netstat
-antp
#
查看所有已经建立的连接
netstat
-s
#
查看
网络统计
信息进程
ps
-ef
#
查看所有进程
top
#
实时显示
进程状态
用户
w
#
查看活动用户
id
#
查看指定用户信息
last
#
查看
用户登录
日志
cut
-d:
-f1
/etc/passwd
#
查看系统所有用户
cut
-d:
-f1
/etc/group
#
查看系统所有组
crontab
-l
#
查看当前用户的计划任务服务
chkconfig
–list
#
列出所有系统服务
chkconfig
–list
|
grep
on
#
列出所有启动的系统服务程序
rpm
-qa
#
查看所有安装的软件包
cat
/proc/cpuinfo
:查看CPU相关参数的linux系统命令
cat
/proc/partitions
:查看linux硬盘和分区信息的系统信息命令
cat
/proc/meminfo
:查看linux系统内存信息的linux系统命令
cat
/proc/version
:查看版本,类似uname
-r
cat
/proc/ioports
:查看设备io端口
cat
/proc/interrupts
:查看中断
cat
/proc/pci
:查看pci设备的信息
cat
/proc/swaps
:查看所有swap分区的信息 参考技术A 美信crecloud云网管,是一款全中文的服务器监控软件,安装、使用非常简单。linux、windows、unix服务器(内存、cpu、进程、性能)监控、监测只要简单配置就可以实现。推荐使用,如果是6台电脑的话,用迷你版还是永久免费哦。
可以搜索美信crecloud云网管下载使用~~
Linux系统内对高CPU的监控及日志分析
使用linux系统时,占用cpu资源过高和,用脚本排查:
1,实时监控,一旦有cpu占用高的进程,程序启动;
2,再对进程分析,得出对应线程;
3,对对应线程所在的程序日志文档进行分析,比如Websphere中间件就有很详备的文件系统;
4,对于日志文件中error,worning等详细查看,但由于有时候日志文件过于庞大,并且容易忽略某些细节,如果用sed和awk,结合四则表达式,可以有效的定位其中的错误并不放过任何细节。
此脚本同,通过一个local脚本和一个remote脚本,能准确监控,并定位日志文件,并分析文件
本地脚本:highCpuAnalysis_l.sh:
准备工作:定义变量
###############################################################################
#The source code is created in 10.19.90.165 and 192.168.86.198
# This script is used to Analysis data for Performance, High CPU Issues on Linux‘
# Usage: ./highCpuAnalysis.sh $IP $USER
# Author: HuangTao
# Email:[email protected]
#
###############################################################################
##########################
# Define Variables #
##########################
export USER=$1;
export IP=$2;
##Usage:
if [ $# -eq 0 ] || [ $# -eq 1 ]
then
echo " Unable to find USER and IP."
echo " Please rerun the script as follows:./highCpuAnalysis.sh USER IP"
echo "eg: ./highCpuAnalysis_l.sh root 192.168.86.198 "
exit 1
fi
##get the remote server‘s WAS application server name
export wasappname=$(ssh [email protected]$IP ps -eo pcpu,pmem,pid,user,args | sort -k 1 -r | sed -n ‘2p‘ |awk ‘{print $NF}‘)
##get the remote server‘s hostname
export remotehostname=$(ssh [email protected]$IP hostname)
##get the current directory
export dir=$(pwd)
第1步:复制本地的分析脚本到远程主机:
###############################################################################
##Copy the script:highCpuAnalysis_r.sh to target host
echo "*********************************************************************"
echo "Step 1: "
echo "copy the highCpuAnalysis_r.sh to the remote host, and "
scp highCpuAnalysis_r.sh [email protected]$IP:/tmp/
ssh [email protected]$IP cd /tmp
ssh [email protected]$IP chmod 755 /tmp/highCpuAnalysis_r.sh
echo "is RUNING on $remotehostname($IP). "
第6步:将远程主机生成的分析文档拷贝到本地主机,并删除远程主机的分析文件
###############################################################################
##run the script, make the script run on target remote host:
ssh [email protected]$IP /tmp/highCpuAnalysis_r.sh
echo "*************************************************************************"
echo "Step 6:"
echo "Copy the report and javacore to the local fenxi host:"
###############################################################################
##Copy the report and javacore to the local host then delete them:
export dir=$(pwd)
scp [email protected]$IP:/tmp/HighCpuReport* .
scp [email protected]$IP:/tmp/javacore*.gz .
tar -zxvf javacore*.gz
##Remove all related files in remate server
ssh [email protected]$IP rm -f /tmp/HighCpu*Report*
ssh [email protected]$IP rm -f /tmp/javacore*
ssh [email protected]$IP rm -f /tmp/highCpuAnaly sis_r.sh
ssh [email protected]$IP rm -f /tmp/topdashH.*
echo " "
第7步:显示分析结果
echo "*********************************************************************"
echo "step 7:"
echo "Show All information:"
echo "Remote hostname: $remotehostname($IP)."
echo "Remote Appserver name:$wasappname."
echo "Report and javacore:"
rm -f javacore*.gz
ls -rlt HighCpu*Report* |tail -1
ls -rtl javacore* |tail -3
echo "*******************************END**********************************"
远程脚本: highCpuAnalysis_r.sh
远程脚本的准备工作:定义远程主机的变量(主要有监控的时间和次数)
###############################################################################
#The source code is created in 10.19.90.165 and 192.168.86.198.
# This script is used to Analysis data for Performance, High CPU Issues on Linux‘
# Usage: ./HighCpuAnalysis.sh
# Author: HuangTao
# Email:[email protected]
###############################################################################
##########################
# Define Variables #
##########################
# How long the top dash H data should be taken in once(second).
TOP_DASH_H_VAL=30
# How many times dash H data should be taken.
TOP_DASH_H_VAL_T=3
# How long one javacores should be taken(second) .
JAVACORE_VAL=60
# How many times javacores should be taken.
JAVACORE_VAL_T=3
##get High CPU pid
此步骤得到占用cpu资源最高的进程ID
export pid=$(ps -eo pcpu,pmem,pid,user,args | sort -k 1 -r | head -10 | sed -n ‘2p‘ |awk ‘{print $3}‘)
##get turn pid number to hexadecimal (from 10 to 16)
export pid16=$(echo "obase=10; $pid" | bc)
##check the pid if WAS process
export was=$(ps -eo pcpu,pmem,pid,user,args | sort -k 1 -r | sed -n ‘2p‘ |awk ‘{print $4}‘)
##get the WAS application name
export wasappname=$(ps -eo pcpu,pmem,pid,user,args | sort -k 1 -r | sed -n ‘2p‘ |awk ‘{print $NF}‘)
##get hostname
export hostname=$(hostname)
第2步:获取占用高CPU的进程PID,并判断是否是特定程序的进程
##########################
# Get High CPU PID #
##########################
## put the report in /tmp/HighCpuReport.$pid.$hostname.out
echo "Script execude time:" $(date) > /tmp/HighCpuReport.$pid.$hostname.out
echo " "
if [ $was = wasuser ] || [ $was = wasadmin ]
then
echo "*********************************************************************"
echo "Step 2:"
echo "The Highest CPU pid is : $pid, the process is WAS porcess. " 判断是否是WAS进程
else
echo "The Highest CPU pid : $pid is NOT WAS process." | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " "
exit 1
fi
sleep 1;
echo "*********************************************************************"
在某一个特定用户名称下(比如wasadmin,httpd),找出占用CPU资源最高的前10项进程:
第3步:分析占用CPU资源最高的某线程中,在特定时间段内线程(或者是子进程)占用情况,并写入到临时文件中,待分析;
#########################
# #
# Start collection of: #
# * top dash H #
# #
#########################
# Start the collection of top dash H data.
echo "Step 3:"
echo "Starting collection of top dash H data ..."
echo "Need $[$TOP_DASH_H_VAL*TOP_DASH_H_VAL_T] seconds to complete this step:"
top -bH -d $TOP_DASH_H_VAL -n $TOP_DASH_H_VAL_T -p $pid > /tmp/topdashH.$pid.$hostname.out
#eg: top -bH -d 30 -n 3 -p 7031
#eg: grep -v Swap toplog.out |grep -v Task |grep -v "Cpu(s)"|grep -v "Mem:" |grep -v top| sort -k 1 -r | head -10 | sed -n ‘2p‘ |awk ‘{print $3}‘
#echo "Analysis the snapshot of /tmp/topdashH.$pid.$hostname.out can find out the hight CPU thread" ;
echo "Collected The top dash H data ."
sleep 2;
第4步:分析上步骤文件,找出某一段时间内消耗CPU资源最多的线程的前10位,(如果CPU占用资源过高是WAS造成的)则同时在指定时间段以内生成JAVACORE供分析。
###########################
# Find out the Thread of most CPU
# and TIME consumner Top 10 .
###########################
##delete the /tmp/topdashH.$pid.$hostname.out when completed the data Collection
############################
# Start collection of: #
# * javacores #
#########################
# Javacores are output to the working directory of the JVM; in most cases this is the <profile_root>
echo "*********************************************************************"
echo "Step 4:"
echo "Starting collection of Javacores ..."
echo "Need $[$JAVACORE_VAL*$JAVACORE_VAL_T] seconds to complete This step:"
##clear the javacore about this PID first:
rm -f /opt/IBM/WebSphere/AppServer/profiles/$wasappname/javacore*$pid*
##then generate the javacore
kill -3 $pid ;
echo "Collected the first javacore for PID $pid ."
sleep $221
kill -3 $pid ;
echo "Collected the second javacore for PID $pid ."
sleep $JAVACORE_VAL
kill -3 $pid ;
echo "Collected the third javacore for PID $pid ."
sleep $JAVACORE_VAL
##mv the javacore to the /tmp DIR and then zip:
rm -f /tmp/javacore*
mv -f /opt/IBM/WebSphere/AppServer/profiles/$wasappname/javacore*$pid* /tmp/
cd /tmp
tar -zcvf javacore.$(date +%Y%m%d"."%H%M%S).$pid.gz javacore*$pid*
第5步:显示分析结果并将分析结果保存到临时文件中
echo "*********************************************************************"
echo "Step 5:"
echo "Print out the Analysis infomantion:"
echo " " | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo "*********The most CPU consumner top 10 PROCESS :*********************" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
ps -eo pcpu,pmem,pid,user,args | sort -k 1 -r |head -10 | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo "*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " "
echo "****The most CPU consumner top 10 *Threads* from process $pid:********" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND " | tee -a /tmp/HighCpuReport.$pid.$hostname.out
cat /tmp/topdashH.$pid.$hostname.out|grep -v Cpu|sort -k9 -n -r -k1 -u |head -10 | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo "*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " " | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo "****The most TIME c onsumner top 10 *Threads* from process $pid:*******" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND " | tee -a /tmp/HighCpuReport.$pid.$hostname.out
cat /tmp/topdashH.$pid.$hostname.out | grep -v Cpu|sort -k11 -n -r -k1 -u |head -10 | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo "*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*" | tee -a /tmp/HighCpuReport.$pid.$hostname.out
echo " "
echo "Pleae check the javacore and HighCpuReport.$pid.$hostname.out under current directory."
以上是关于如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息的主要内容,如果未能解决你的问题,请参考以下文章