11、snmp监控window和Linux

版权声明:原创作品,谢绝转载!否则将追究法律责任。 ————— 作者:kirin

应用:

​ 监控网络设备 (常用)

​ 也支持监控可以启动SNMP功能的设备(windows,linux,打印机........)

15.1.监控网络设备

​ 1.监控网络设备

​ 2.zabbix服务端进行测试 能否获取到 网络设备的信息

​ 3.web添加主机,监控项......

不能安装zabbix-agent的设备,都可以使用snmp监控

snmp协议: simple network manager protocol

OID:监控标准都有一个唯一的id,object id

总内存oid: .1.3.6.1.4.1.2021.4.5.0

MIB:

存储所有oid信息

snmp协议版本

v1

v2c -c commuity 密码,口令、通过 团体名id 访问设备 访问 snmp信息

v3 通过用户名和密码 访问 snmp信息

#配置团体名字

#zabbix-server检查 网络设备状态

[root@zabbix-server ~]# yum install  -y net-snmp-utils

COMMUNITY

[root@zabbix-server ~]# snmpwalk -c oldboykx -v 2c 192.168.13.1 sysDesc
SNMPv2-MIB::sysDescr.0 = STRING: H3C Product Version ERHMG2-MNW100-R1117
H3C ERHMG2
Copyright(c) 2014-2018 New H3C Technologies Co., Ltd. All rights reserved.
[root@m03 ~]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 sysdesc
SNMPv2-MIB::sysDescr.0 = STRING: H3C Product Version ERHMG2-MNW100-R1117
H3C ERHMG2
Copyright(c) 2014-2018 New H3C Technologies Co., Ltd. All rights reserved.

snmpwalk 命令 使用get方式访问网络设备
-c 团体名称	#v2c版本使用 团体名称
-v 指定 snmp版本 #v2c  v3
ip地址
指令(获取网络设备的信息)	名称方式/oid

#oid  Object ID 事务id 给世间万物设置独一无二的id号

[root@zabbix-server  /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 sysdesc
[root@zabbix-server  /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 sysUptime
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (40733900) 4 days, 17:08:59.00
[root@zabbix-server  /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 IfNumber
IF-MIB::ifNumber.0 = INTEGER: 19
[root@m03 /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 IfDescr
IF-MIB::ifDescr.16777217 = STRING: WAN1
IF-MIB::ifDescr.16777218 = STRING: WAN2
IF-MIB::ifDescr.50331650 = STRING: LAN1
IF-MIB::ifDescr.50331652 = STRING: LAN2
IF-MIB::ifDescr.50331654 = STRING: LAN3
IF-MIB::ifDescr.50331656 = STRING: LAN4
IF-MIB::ifDescr.385875968 = STRING: VLAN1
IF-MIB::ifDescr.385875969 = STRING: VLAN11
IF-MIB::ifDescr.385875970 = STRING: VLAN12
IF-MIB::ifDescr.385875971 = STRING: VLAN13
IF-MIB::ifDescr.385875972 = STRING: VLAN14
IF-MIB::ifDescr.385875973 = STRING: VLAN15
IF-MIB::ifDescr.385875974 = STRING: VLAN16
IF-MIB::ifDescr.385875975 = STRING: VLAN17
IF-MIB::ifDescr.385875976 = STRING: VLAN18
IF-MIB::ifDescr.385875977 = STRING: VLAN19
IF-MIB::ifDescr.385875978 = STRING: VLAN20
IF-MIB::ifDescr.385875979 = STRING: VLAN21
IF-MIB::ifDescr.385875980 = STRING: VLAN66
[root@m03 /tmp]#
[root@m03 /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 IfInOctet
IF-MIB::ifInOctets.16777217 = Counter32: 4096624309
IF-MIB::ifInOctets.16777218 = Counter32: 0
IF-MIB::ifInOctets.50331650 = Counter32: 3656051160
IF-MIB::ifInOctets.50331652 = Counter32: 3255019547
IF-MIB::ifInOctets.50331654 = Counter32: 4121087927
IF-MIB::ifInOctets.50331656 = Counter32: 0
IF-MIB::ifInOctets.385875968 = Counter32: 121007621
IF-MIB::ifInOctets.385875969 = Counter32: 1314395390
IF-MIB::ifInOctets.385875970 = Counter32: 2701446525
IF-MIB::ifInOctets.385875971 = Counter32: 3113202231
IF-MIB::ifInOctets.385875972 = Counter32: 3558929335
IF-MIB::ifInOctets.385875973 = Counter32: 0
IF-MIB::ifInOctets.385875974 = Counter32: 0
IF-MIB::ifInOctets.385875975 = Counter32: 0
IF-MIB::ifInOctets.385875976 = Counter32: 0
IF-MIB::ifInOctets.385875977 = Counter32: 4039834871
IF-MIB::ifInOctets.385875978 = Counter32: 3986752945
IF-MIB::ifInOctets.385875979 = Counter32: 1748855398
IF-MIB::ifInOctets.385875980 = Counter32: 0
[root@m03 /tmp]# snmpwalk	-c oldboykx -v 2c  192.168.13.1 IfInOctet
IF-MIB::ifInOctets.16777217 = Counter32: 1402719161
……………………

#添加主机




小结:

启动 设备的SNMP功能 (网络设备) 安装并启动 snmp服务 端口161

zabbix服务端进行测试 能否获取到 网络设备的信息 snmpwalk

web添加主机,关联模板......

15.2.snmp监控linux

#使用snmp协议监控linux
1:安装snmp服务端
yum install net-snmp -y

2:配置snmp服务端 vim /etc/snmp/snmpd.conf
#修改community为oldzhang
com2sec notConfigUser default       oldzhang
#增加一行
view   systemview   included   .1

3:启动snmp
systemctl start snmpd.service 
systemctl enable snmpd.service 
4:安装snmp客户端并测试取值
yum install net-snmp-utils.x86_64 -y
snmpwalk -v 2c -c oldboy 10.0.0.8 .1.3.6.1.4.1.2021.4.5.0
-v 指定snmp协议版本 参考snmp服务端
-c 指定密码字符串 
#OID Object id 任何事事情配置了一个id

[root@web01 ~]# snmpwalk -v 2c -c oldboy 10.0.0.7   .1.3.6.1.4.1.2021.4.5.0
UCD-SNMP-MIB::memTotalReal.0 = INTEGER: 3861272 kB
[root@web01 ~]# snmpwalk -v 2c -c oldboy 10.0.0.7   memTotalReal
UCD-SNMP-MIB::memTotalReal.0 = INTEGER: 3861272 k

15.3.监控Windows

win7:



win10参考:




#配置snmp

[root@web01 ~]# snmpwalk -v 2c -c oldboy 10.0.0.1   SysName
SNMPv2-MIB::sysName.0 = STRING: DESKTOP-9RU7VPJ
[root@web01 ~]# snmpwalk -v 2c -c oldboy 10.0.0.1   .1.3.6.1.2.1.1.1.0
SNMPv2-MIB::sysDescr.0 = STRING: Hardware: Intel64 Family 6 Model 94 Stepping 3 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
snmp监控linux

#在web01上安装snmp服务端

[root@web01 ~]# yum install net-snmp -y

#修改文件第41行
[root@web01 ~]# grep oldzhang /etc/snmp/snmpd.conf
com2sec notConfigUser  default      oldzhang
#修改客户端权限,添加下面这一行(在第56行添加)
view    systemview    included   .1

#启动服务
[root@web01 ~]# systemctl start snmpd.service
[root@web01 ~]# systemctl enable snmpd.service

#检查启动端口为udp协议的161端口
[root@web01 ~]# ss -lntup|grep snmp

#安装snmp客户端,测试取值
[root@zabbix-server ~]# yum install net-snmp-utils.x86_64 -y
[root@zabbix-server ~]# snmpwalk -v 2c -c oldzhang 10.0.0.7 .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 479596 Kbytes
#snmpget 每次只能取一个值
#snmpwalk 范围性的取值

#新建主机,链接模板




15.4.JMX

1、概述

​ 监控java程序 java-gateway

2、环境准备

​ web02

​ jdk tomcat

​ 开启远程监控功能

[root@web02 ~]# java -version 
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
[root@web02 ~]# /app/tomcat/bin/version.sh 
Using CATALINA_BASE:   /app/tomcat
Using CATALINA_HOME:   /app/tomcat
Using CATALINA_TMPDIR: /app/tomcat/temp
Using JRE_HOME:       /app/jdk
Using CLASSPATH:       /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:                         -Dcom.sun.management.jmxremote                     -
Dcom.sun.management.jmxremote.port=12345          -Dcom.sun.management.jmxremote.authenticate=false  -
Dcom.sun.management.jmxremote.ssl=false           -Djava.rmi.server.hostname=10.0.0.9
Server version: Apache Tomcat/8.5.66
Server built:   May 8 2021 22:44:01 UTC
Server number:  8.5.66.0
OS Name:       Linux
OS Version:     3.10.0-1127.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_60-b27
JVM Vendor:     Oracle Corporation

#开启tomcat远程监控 功能  
#修改 tomcat/bin/catalina.sh   

#CATALINA_OPTS java内置变量 修改java启动参数(tomcat) 
# CATALINA_OPTS java启动参数   

CATALINA_OPTS="$CATALINA_OPTS                 
-Dcom.sun.management.jmxremote                      #jmx remote   开启tomcat远程监控功能 
-Dcom.sun.management.jmxremote.port=12345           #指定端口 12345 还有2个随机端口
-Dcom.sun.management.jmxremote.authenticate=false  #auth 认证 是否开启远程监控认证(用户名 密码)
-Dcom.sun.management.jmxremote.ssl=false           #是否开启https 
-Djava.rmi.server.hostname=10.0.0.7"             #tomcat监听的ip地址  
                      #这里我们书写公网ip地址
                      #生产环境 书写内网ip 
                      #本地ip 10.0.0.7 172.16.1.7
#tomcat/bin/catalina.sh    
CATALINA_OPTS="$CATALINA_OPTS                 
-Dcom.sun.management.jmxremote                    
-Dcom.sun.management.jmxremote.port=12345         
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false          
-Djava.rmi.server.hostname=10.0.0.7"                                   
#
[root@web02 ~]# vim /app/tomcat/bin/catalina.sh 
127 CATALINA_OPTS="$CATALINA_OPTS                     \
128 -Dcom.sun.management.jmxremote                     \
129 -Dcom.sun.management.jmxremote.port=12345         \
130 -Dcom.sun.management.jmxremote.authenticate=false \
131 -Dcom.sun.management.jmxremote.ssl=false           \
132 -Djava.rmi.server.hostname=10.0.0.8”


[root@web02 ~]# ps -ef |grep java 
root      32902  32652  0 15:48 pts/0    00:00:00 grep --color=auto java
[root@web02 ~]# ss -lntup |grep java 
[root@web02 ~]# /app/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /app/tomcat
Using CATALINA_HOME:   /app/tomcat
Using CATALINA_TMPDIR: /app/tomcat/temp
Using JRE_HOME:       /app/jdk
Using CLASSPATH:       /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:                         -Dcom.sun.management.jmxremote                     -
Dcom.sun.management.jmxremote.port=12345          -Dcom.sun.management.jmxremote.authenticate=false  -
Dcom.sun.management.jmxremote.ssl=false           -Djava.rmi.server.hostname=10.0.0.8
Tomcat started.
[root@web02 ~]# systemctl start tomcat.service 
Display all 170 possibilities? (y or n)
[root@web02 ~]# systemctl cat tomcat.service 
# /usr/lib/systemd/system/tomcat.service
[Unit]
Description=tomcat 8080 system cmd 
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/app/tomcat/bin/startup.sh
ExecStop=/app/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target 
[root@web02 ~]# ss -lntup |grep java 
tcp   LISTEN     0      100   [::]:8080               [::]:*                   users:
(("java",pid=32957,fd=56))
tcp   LISTEN     0      50     [::]:34423             [::]:*                   users:
(("java",pid=32957,fd=21))
tcp   LISTEN     0      50     [::]:12345             [::]:*                   users:
(("java",pid=32957,fd=20))
tcp   LISTEN     0      50     [::]:43752             [::]:*                   users:
(("java",pid=32957,fd=19))

3、zabbix-java-gateway (zabbix服务端)

[root@zabbix-server ~]# yum install -y zabbix-java-gateway
[root@zabbix-server ~]# rpm -ql zabbix-java-gateway 
/etc/zabbix/zabbix_java_gateway.conf                  #zabbix-java-gateway 配置文件
/etc/zabbix/zabbix_java_gateway_logback.xml
/usr/lib/systemd/system/zabbix-java-gateway.service     #systemctl 管理配置
/usr/lib/tmpfiles.d/zabbix-java-gateway.conf
/usr/sbin/zabbix_java_gateway
/usr/share/zabbix-java-gateway
/usr/share/zabbix-java-gateway/bin
/usr/share/zabbix-java-gateway/bin/zabbix-java-gateway-5.0.13.jar
/usr/share/zabbix-java-gateway/lib                             #库文件 依赖
/usr/share/zabbix-java-gateway/lib/android-json-4.3_r3.1.jar
/usr/share/zabbix-java-gateway/lib/logback-classic-1.2.3.jar
/usr/share/zabbix-java-gateway/lib/logback-core-1.2.3.jar
/usr/share/zabbix-java-gateway/lib/slf4j-api-1.7.30.jar
/var/log/zabbix
/var/run/zabbix

#修改zabbix-java-gateway 配置文件
/etc/zabbix/zabbix_java_gateway.conf

[root@zabbix-server~]# grep '^[a-Z]' /etc/zabbix/zabbix_java_gateway.conf 
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java.pid"

[root@zabbix-server~]# egrep -n '^JavaG|^StartJ' /etc/zabbix/zabbix_server.conf 
297:JavaGateway=127.0.0.1
305:JavaGatewayPort=10052
313:StartJavaPollers=5 
[root@zabbix-server ~]# systemctl enable zabbix-java-gateway.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-java-gateway.service to 
/usr/lib/systemd/system/zabbix-java-gateway.service.
[root@zabbix-server~]# systemctl restart zabbix-java-gateway.service   zabbix-server.service

#检查进程信息
[root@zabbix-server ~]# ps auxf |grep java-gateway 
root      11073  0.0  0.0 112808   972 pts/0   S+   16:03   0:00         \_ grep --color=auto javagateway
zabbix    10951  0.9  1.1 2975420 42768 ?       Sl   16:02   0:00 java -server -
Dlogback.configurationFile=/etc/zabbix/zabbix_java_gateway_logback.xml -classpath lib:lib/android-json4.3_r3.1.jar:lib/logback-classic-1.2.3.jar:lib/logback-core-1.2.3.jar:lib/slf4j-api-1.7.30.jar:bin/zabbixjava-gateway-5.0.13.jar -Dzabbix.pidFile=/var/run/zabbix/zabbix_java.pid -Dzabbix.listenPort=10052 -
Dsun.rmi.transport.tcp.responseTimeout=3000 com.zabbix.gateway.JavaGateway

#检查端口
[root@zabbix-server ~]# ss -lntup |grep java 
tcp   LISTEN     0      50     [::]:10052             [::]:*                   users:
(("java",pid=10951,fd=12))
[root@zabbix-server ~]# ss -lntup |grep zabbix 

4、调试

[root@zabbix-server ~]# java -jar cmdline-jmxclient-0.10.3.jar - 10.0.0.8:12345
[root@zabbix-server ~]# echo $?

5、添加主机 web02



6、jmx监控 小结

监控java方案01 : zabbix-java-gateway + jmx + 内置模板

监控java方案02: 通过jmap/jstat 实现自定义监控 (不需要安装java-gateway,不需要开启远程监控功能)'

#监控java方案02 脚本思路 (自定义脚本)
1. 用户输入参数 tomcat8080 (应用名称) 什么名字
2. 根据应用找出pid
3. 根据pid导出jmap -heap pid jvm内存使用情况
4. 进行过滤 过滤出指定 名字的内存空间使用率

[root@web02 ~]# jps -lvm  
33756 sun.tools.jps.Jps -lvm -Denv.class.path=.:/app/jdk/lib:/app/jdk/jre/lib:/app/jdk/lib/tools.jar -
Dapplication.home=/app/jdk -Xms8m
32957 org.apache.catalina.startup.Bootstrap start -
Djava.util.logging.config.file=/app/tomcat/conf/logging.properties -
Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -
Djava.protocol.handler.pkgs=org.apache.catalina.webresources -
Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dcom.sun.management.jmxremote -
Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -
Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.8 -Dignore.endorsed.dirs= -
Dcatalina.base=/app/tomcat -Dcatalina.home=/app/tomcat -Djava.io.tmpdir=/app/tomcat/temp

[root@web02 ~]# jmap -heap 32957
Attaching to process ID 32957, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.60-b23
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 989855744 (944.0MB)
   NewSize                  = 20971520 (20.0MB)
   MaxNewSize               = 329908224 (314.625MB)
   OldSize                  = 41943040 (40.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 18939904 (18.0625MB)
   used     = 1390704 (1.3262786865234375MB)
   free     = 17549200 (16.736221313476562MB)
   7.342719371756055% used
Eden Space:
   capacity = 16842752 (16.0625MB)
   used     = 748576 (0.713897705078125MB)
   free     = 16094176 (15.348602294921875MB)
   4.444499331225681% used
From Space:
capacity = 2097152 (2.0MB)
   used     = 642128 (0.6123809814453125MB)
   free     = 1455024 (1.3876190185546875MB)
   30.619049072265625% used
To Space:
   capacity = 2097152 (2.0MB)
   used     = 0 (0.0MB)
   free     = 2097152 (2.0MB)
   0.0% used
tenured generation:
   capacity = 41943040 (40.0MB)
   used     = 12095128 (11.534812927246094MB)
   free     = 29847912 (28.465187072753906MB)
   28.837032318115234% used
16412 interned Strings occupying 1395720 bytes. 

15.5zabbix客户端常见类型

posted @ 2022-06-17 21:48  kirin(麒麟)  阅读(1205)  评论(0编辑  收藏  举报
Δ