shell脚本采集系统cpu、内存、磁盘、网络信息
有不少朋友不知道如何用shell脚本采集linux系统相关信息,包括cpu、内存、磁盘、网络等信息,这里脚本小编做下讲解,大家一起来看看吧。
一、cpu信息采集
1),采集cpu使用率
采集算法:通过/proc/stat文件采集并计算CPU总使用率或者单个核使用率。以cpu0为例,算法如下:
1,cat /proc/stat | grep ‘cpu0'得到cpu0的信息
2,cpuTotal1=user+nice+system+idle+iowait+irq+softirq
3,cpuUsed1=user+nice+system+irq+softirq
4,sleep 30秒
5,再次cat /proc/stat | grep 'cpu0' 得到cpu的信息
6,cpuTotal2=user+nice+system+idle+iowait+irq+softirq
7,cpuUsed2=user+nice+system+irq+softirq
8,得到cpu0 在30秒内的单核利用率:(cpuUsed2 – cpuUsed1) * 100 / (cpuTotal2 – cpuTotal1)
相当于使用top –d 30命令,把user、nice、system、irq、softirq五项的使用率相加。
shell代码:
a=(`cat /proc/stat | grep -E "cpu\b" | awk -v total=0 '{$1="";for(i=2;i<=NF;i++){total+=$i};used=$2+$3+$4+$7+$8 }END{print total,used}'`)
sleep 30
b=(`cat /proc/stat | grep -E "cpu\b" | awk -v total=0 '{$1="";for(i=2;i<=NF;i++){total+=$i};used=$2+$3+$4+$7+$8 }END{print total,used}'`)
cpu_usage=(((${b[1]}-${a[1]})*100/(${b[0]}-${a[0]})))
2),采集cpu负载
采集算法:读取/proc/loadavg得到机器的1/5/15分钟平均负载,再乘以100。
shell代码:
cpuload=(`cat /proc/loadavg | awk '{print $1,$2,$3}'`)
load1=${cpuload[0]}
load5=${cpuload[1]}
load15=${cpuload[2]}
二、内存采集
1).应用程序使用内存
采集算法:读取/proc/meminfo文件,(MemTotal – MemFree – Buffers – Cached)/1024得到应用程序使用内存数。
shell代码:
awk '/MemTotal/{total=$2}/MemFree/{free=$2}/Buffers/{buffers=$2}/^Cached/{cached=$2}END{print (total-free-buffers-cached)/1024}' /proc/meminfo
2).MEM使用量
采集算法:读取/proc/meminfo文件,MemTotal – MemFree得到MEM使用量。
shell代码:
awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
3).SWAP使用大小
采集算法:通过/proc/meminfo文件,SwapTotal – SwapFree得到SWAP使用大小。
shell代码:
awk '/SwapTotal/{total=$2}/SwapFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
三、磁盘信息采集(disk io)
1、IN:平均每秒把数据从硬盘读到物理内存的数据量
采集算法:读取/proc/vmstat文件得出最近240秒内pgpgin的增量,把pgpgin的增量再除以240得到每秒的平均增量。
相当于vmstat 240命令bi一列的输出。
shell代码:
a=`awk '/pgpgin/{print $2}' /proc/vmstat`
sleep 240
b=`awk '/pgpgin/{print $2}' /proc/vmstat`
ioin=$(((b-a)/240))
2、OUT:平均每秒把数据从物理内存写到硬盘的数据量
采集算法:读取/proc/vmstat文件得出最近240秒内pgpgout的增量,把pgpgout的增量再除以240得到每秒的平均增量。
相当于vmstat 240命令bo一列的输出。
shell代码:
a=`awk '/pgpgout/{print $2}' /proc/vmstat`
sleep 240
b=`awk '/pgpgout/{print $2}' /proc/vmstat`
ioout=$(((b-a)/240))
四、采集网络流量
1).流量
以http://www.jquerycn.cn/为例,eth0是内网,eth1外网,获取60秒的流量。
机器网卡的平均每秒流量
采集算法:读取/proc/net/dev文件,得到60秒内发送和接收的字节数(KB),然后乘以8,再除以60,得到每秒的平均流量。
shell代码:
traffic_be=(`awk 'BEGIN{ORS=" "}/eth0/{print $2,$10}/eth1/{print $2,$10}' /proc/net/dev`)
sleep 60
traffic_af=(`awk 'BEGIN{ORS=" "}/eth0/{print $2,$10}/eth1/{print $2,$10}' /proc/net/dev`)
eth0_in=$(( (${traffic_af[0]}-${traffic_be[0]})/60 ))
eth0_out=$(( (${traffic_af[1]}-${traffic_be[1]})/60 ))
eth1_in=$(( (${traffic_af[2]}-${traffic_be[2]})/60 ))
eth1_out=$(( (${traffic_af[3]}-${traffic_be[3]})/60 ))
2).包量
机器网卡的平均每秒包量
采集算法:读取/proc/net/dev文件,得到60秒内发送和接收的包量,然后除以60,得到每秒的平均包量。
shell代码:
packet_be=(`awk 'BEGIN{ORS=" "}/eth0/{print $3,$11}/eth1/{print $3,$11}' /proc/net/dev`)
sleep 60
packet_af=(`awk 'BEGIN{ORS=" "}/eth0/{print $3,$11}/eth1/{print $3,$11}' /proc/net/dev`)
eth0_in=$(( (${packet_af[0]}-${packet_be[0]})/60 ))
eth0_out=$(( (${packet_af[1]}- ${packet_be[1]})/60 ))
eth1_in=$(( (${packet_af[2]}- ${packet_be[2]})/60 ))
eth1_out=$(( (${packet_af[3]}- ${packet_be[3]})/60 ))
shell脚本采集系统cpu、内存、磁盘、网络信息的更多相关文章
- Shell采集系统cpu 内存 磁盘 网络信息
cpu信息采集 cpu使用率 采集算法 通过/proc/stat文件采集并计算CPU总使用率或者单个核使用率.以cpu0为例,算法如下: 1. cat /proc/stat | grep ‘cpu0’ ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
- shell脚本监控cpu/内存使用率 转
该脚本检测cpu和内存的使用情况,只需要调整memorySetting.cpuSetting.userEmail要发邮件报警的email地址即可 如果没有配置发邮件参数的哥们,已配置了的,直接飞到代码 ...
- 获取并检查系统负载\CPU\内存\磁盘\网络
安装依赖 需要net-tools.namp! CentOS:yum -y install net-tools nmap Ubuntu:apt-get update && apt-get ...
- jmeter ---监控服务器CPU, 内存,网络数据
JMeter如何收集获得服务器cpu,内存,磁盘,网络等相关资源使用率的信息 1. JMeter 自带的Monitor Results 监控 JMeter 自带的Monitor Results 在官网 ...
- Shell脚本实现检测某ip网络畅通情况,实战用例
Shell脚本实现检测某ip网络畅通情况,实战用例 环境准备,linux shell 发送email 邮件:1.安装sendmailyum -y install sendmail安装好sendmail ...
- DSAPI 获取实时统计信息CPU/内存/硬盘/网络
有时,我们需要获取当前计算机中CPU.内存.硬盘.网络等实时信息,如下图:\ 要实现上述几项信息的获取,通常需要使用Timer控件来间隔获取,以便刷新最新的数据. 本示例中,放一个Timer控件,放一 ...
- AIX/Linux/HP-UX查看CPU/内存/磁盘/存储命令
1.1 硬件环境验证方式 硬件环境主要包括CPU.内存.磁盘/存储.网络设备(如F5等).系统特有设备(如密押设备等)等,其中网络设备和系统特有设备由网络管理员或项目组提供为准,本节主要关注CP ...
- 在CentOS6.9上Shell脚本定时释放内存cache
一.写Shell脚本 mkdir -p /var/script/ vim /var/script/freemem.sh 写入以下Shell脚本: #!/bin/bash # 当前已使用的内存大小 us ...
随机推荐
- Java测试开发--Java基础知识(二)
一.java中8大基本类型 数值类型:byte.short.int .float.double .long 字符类型:char 布尔类型:boolean 二. 封装:将属性私有化,不允许外部数据直接访 ...
- SpringCloud 2020.0.4 系列之 Sleuth + Zipkin
1. 概述 老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策. 言归正传,微服务的特点就是服务多,服务间的互相调用也很复杂,就像一张关系网,因此为了更好的定位故障和优 ...
- pyinstaller打包:AttributeError: module ‘win32ctypes.pywin32.win32api’ has no attribute ‘error’
pyinstaller打包:AttributeError: module 'win32ctypes.pywin32.win32api' has no attribute 'error' 是因为pyin ...
- js 实现匀速移动
js 实现匀速移动 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- C#生成新浪微博短网址 示例源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DotN ...
- 如何修改visual-studio的sln文件和project工程名
关于VS的 .sln 文件和 .suo 文件 *.sln:(Visual Studio.Solution) 通过为环境提供对项目.项目项和解决方案项在磁盘上位置的引用,可将它们组织到解决方案中.比如是 ...
- IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法
出现这种错误可以在pom.xml里配置, 找到<build>标签在下面<plugins>标签上面加上<defaultGoal>compile</default ...
- 快速搭建 kvm web 管理工具 WebVirtMgr
作者:SRE运维博客 博客地址: https://www.cnsre.cn/ 文章地址:https://www.cnsre.cn/posts/211117937177/ 相关话题:https://ww ...
- Python 爬取 妹子图(技术是无罪的)
... #!/usr/bin/env python import urllib.request from bs4 import BeautifulSoup def crawl(url): header ...
- 【JAVA】编程(6)--- 应用IO流拷贝文件夹(内含多个文件)到指定位置
此程序应用了: File 类,及其常用方法: FileInputStream,FileOutputStream类及其常用方法: 递归思维: package com.bjpowernode.javase ...