首页 > 其他分享 >配置snmptrap服务器写入日志并通过邮件报警

配置snmptrap服务器写入日志并通过邮件报警

时间:2023-08-19 15:56:10浏览次数:36  
标签:bin log 写入 snmptrap sh trap file 日志 LOGFILE

配置snmptrap服务器写入日志并通过邮件报警

  1. 安装相关软件包

    yum install net-snmp net-snmp-utils mailx
    
  2. 修改snmptrapd配置文件/etc/snmp/snmptrapd.conf

    disableAuthorization yes
    authCommunity log,execute,net public
    traphandle default /usr/local/bin/traplog.sh
    
  3. 创建traplog.sh脚本,将trap信息写入到日志文件

    vim /usr/local/bin/traplog.sh
    chmod +x /usr/local/bin/traplog.sh
    

    文件内容如下

    #!/bin/bash
    # traplog.sh
    # A script to log trap information to a file
    
    # Define the log file path
    LOGFILE=/var/log/snmp/trap.log
    
    # Get the current datetime
    DATE=$(date +"%Y-%m-%d %H:%M:%S")
    
    # Write a header to the log file
    touch $LOGFILE
    echo "------------------------------" >> $LOGFILE
    echo "Trap received at $DATE" >> $LOGFILE
    
    # Read the trap information from standard input and write it to the log file
    while read line
    do
        echo "$line" >> $LOGFILE
    done
    
    # Write a footer to the log file
    echo "End of trap" >> $LOGFILE
    
  4. 配置防火墙并启动snmptrapd服务

    firewall-cmd --add-port=162/udp --permanent
    firewall-cmd --reload
    systemctl start snmptrapd.service
    systemctl enable snmptrapd.service
    
  5. 创建脚本监听日志文件并发送邮件

    touch /usr/local/bin/trapmail.sh
    chmod +x /usr/local/bin/trapmail.sh
    

    文件内容如下

    #!/bin/bash
    
    # Script name: trapmail.sh
    # Author: wanghongwei
    # Date: 2023-08-18
    # Version: 1.0
    # Description: A script to monitor trap and send email alerts
    # Usage: ./trapmail.sh
    
    # Define lockfile and add exclusive lock
    LOCKFILE=/var/run/trapmail.lock
    exec 200>$LOCKFILE
    flock -n 200
    if [ $? != 0 ]; then
    	echo "Fatal: The script is already running!" && exit 1
    fi
    
    # Define the logfiles
    LOGFILE=/var/log/snmp/trapmail.log
    TRAPLOG=/var/log/snmp/trap.log
    
    # Define the email subject and recipient
    SUBJECT="SNMP Trap Alert"
    RECIPIENT="[email protected]"
    
    # Get the last modified time of the file
    LASTMOD=$(stat $TRAPLOG | grep Modify | cut -d ' ' -f 2,3)
    
    # Loop forever
    while true; do
        # Get the current modified time of the file
    	CURMOD=$(stat $TRAPLOG | grep Modify | cut -d ' ' -f 2,3)
    
    	# Compare the current and last modified time
    	if [ "$CURMOD" != "$LASTMOD" ]; then
            # If the file has changed, update the last modified time
            LASTMOD=$CURMOD
    		# Get the current datetime and recording
    		DATE=$(date +"%Y-%m-%d %H:%M:%S")
    		echo "$DATE Info: The traplog file has changed." >>$LOGFILE
    		# Get the last trap of the file, which is the new trap information
    		STACK=""
    		while read line; do
      		    if [[ $line =~ ^-----.* ]]; then
        	        break
      		    else
        			STACK="$STACK\n$line"
      			fi
    		done< <(tac $TRAPLOG)
    		TRAP=$(echo -e $STACK | tac)
    
    		# Send the trap information as email to the recipient
    		echo "$TRAP" | mailx -s "$SUBJECT" -a $TRAPLOG $RECIPIENT
    		echo "$DATE Info: New trap received and sent to $RECIPIENT." >>$LOGFILE
    	fi
    
    	# Sleep for 10 seconds before checking again
    	sleep 10
    done
    
    # Release exclusive lock
    exec 200>&-
    
  6. 启动脚本

    /usr/local/bin/trapmail.sh &
    
  7. 发送测试报文验证

    snmptrap -v 2c -c public 127.0.0.1:162 "" .1.3.6.1.4.1.2021.251.1 sysLocation.0 s "Shanghai" sysName.0 s "monitor.example.com"
    

标签:bin,log,写入,snmptrap,sh,trap,file,日志,LOGFILE
From: https://www.cnblogs.com/wanghongwei-dev/p/17642557.html

相关文章

  • 部署Kafka+ZK及其日志采集实战(系统版本:linux_CentOs_7.8)
    部署ZKdockerrun-d--namezookeeper-p2181:2181-twurstmeister/zookeeper部署Kafka-p9092:9092\-eKAFKA_BROKER_ID=0\--envKAFKA_HEAP_OPTS=-Xmx256M\--envKAFKA_HEAP_OPTS=-Xms128M\-eKAFKA_ZOOKEEPER_CONNECT=[内网ip]:2181\-eKAFKA_ADVERTISED......
  • 日志等级类的测试
    日志等级类的测试枚举这里并没有什么要测试的地方,主要测试的地方就是转换的接口,上面报错的地方也就是说tostring是类的成员函数,使用的时候需要先定义一个类的对象出来,但是这个函数不需要传递this指针,所以可以定义成static静态成员函数打印:再对头文件进行修改,防止重复包含:/......
  • 使用Nlog日志
    NLog是一个基于.NET平台编写的日志记录类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码。可以在任何一种.NET语言中输出带有上下文的(contextualinformation)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中--《Nlog详解》By龙腾一组至尊龙......
  • 批量备份数据库日志且30天后自动删除该备份文件
    /********************************************批量备份数据库日志且30天后自动删除该备份文件*******************************************/DECLARE@backupfileVARCHAR(1024)DECLARE@filenameVARCHAR(1024)DECLARE@pathVARCHAR(1024)DECLARE@dbnameVARC......
  • 为WPF框架Prism注册Nlog日志服务
    这篇文章介绍了为WPF框架Prism注册Nlog日志服务的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 无论是Nlog还是Serilog,它们都提供了如何快速在各类应用程序当中的快速使用方法。尽管,你现在无论是在WPF或者ASP.NETCore当中,......
  • grafana发送日志告警
    参考文档:Grafana告警配置参考:https://www.jianshu.com/p/dcc1af87ccf1Grafana告警配置参考:https://www.yii666.com/blog/516251.html企业微信开发参考:http://www.taodudu.cc/news/show-3254375.html?action=onClick1、grafana创建新的data_sources填写完data_sources......
  • docker查看日志的三种方式
    docker查看日志的三种方式:1.dockerlogs--tail=1000容器名称(查看容器前多少行的日志)2.docker容器启动后,可以进入以下位置查看日志(/var/lib/docker/containers/容器ID/容器ID-json.log)(进入容器内部查看日志)3.dockerattach容器名称(实时查看,但是CTRL+C强制退出以后也会影响......
  • golang Gin框架 自定义日志形式
    funcmain(){ router:=gin.New() //LoggerWithFormattermiddlewarewillwritethelogstogin.DefaultWriter //Bydefaultgin.DefaultWriter=os.Stdout router.Use(gin.LoggerWithFormatter(func(paramgin.LogFormatterParams)string{ //yourcustomfo......
  • Java日志系列:日志门面JCL、SLF4J
    目录一、日志门面说明二、JCL使用JCL入门JCL原理三、SLF4J使用配合自身简单日志实现(slf4j-simple)配置logback日志实现配置Log4J日志实现(需适配器)配置JUL日志实现(需适配器)添加slf4j-nop依赖(日志开关)桥接旧的日志实现框架一、日志门面说明当我们的系统变的更加复杂的......
  • 3.2.0 版本预告!远程日志解决 Worker 故障获取不到日志的问题
    ApacheDolphinScheduler3.2.0版本已经呼之欲出,8月中下旬,这个大版本就要和用户见面了。为了让大家提前了解到此版本更新的主要内容,我们已经制作了几期视频和内容做了大致介绍,包括《重磅预告!ApacheDolphinScheduler3.2.0新功能“剧透”》、《3.2.0版本预告!ApacheDolphinSc......