gpt4 book ai didi

linux - Cacti 通过自定义脚本扩展 snmp

转载 作者:太空狗 更新时间:2023-10-29 11:18:50 25 4
gpt4 key购买 nike

我在 Cacti 上创建了一个关于使用 cacti 和 net-snmp 访问我们基础设施上特定页面的时间的图表。

我通过在文件/etc/snmp/snmpd.conf 中添加两行来扩展功能:

extend stat_page1 /usr/local/bin/cacti/access_page.sh context1
extend stat_page2 /usr/local/bin/cacti/access_page.sh context2

我已重新启动守护程序 snmpd 以加载此配置。

调用的脚本如下所述,具有其他值,因为出于某种原因,我可以显示它。

#!/bin/bash

domain="mydomain"
cookie_name="myCookie"
token="myToken"
if [ $# -eq 1 ]
then

if [ "$1" = "context1" ]
then

target_url="https://${domain}/${1}/page1.html"
TIME=$(curl -s -w "%{time_total}" -o /dev/null --cookie \"${cookie_name}=${token}\" ${target_url})
echo "$TIME"
elif [ "$1" = "context2" ]
then
target_url="https://${domain}/${1}/page2.html"
TIME=$(curl -s -w "%{time_total}" -o /dev/null --cookie \"${cookie_name}=${token}\" ${target_url})
echo "$TIME"
fi

如果我手动启动脚本我有这个

$ /usr/local/bin/cacti/access_page.sh context2
0.061
$ /usr/local/bin/cacti/access_page.sh context1
0.041

当我使用 snmpget 启动脚本时,我得到了这个结果:

snmpwalk -v2c -c myCommunity localhost NET-SNMP-EXTEND-MIB::nsExtendOutput2Table 
NET-SNMP-EXTEND-MIB::nsExtendOutLine."stat_page1".1 = STRING: 0.000
NET-SNMP-EXTEND-MIB::nsExtendOutLine."stat_page2".1 = STRING: 0.000

一直以来,我都通过 snmp 命令获得 0.000 值并手动获得真实值。

你能帮我一下吗?,拜托

最佳答案

最近我遇到了 snmp 和调用 curl 命令的执行脚本的问题。

当我搜索一些解决方案时,这篇文章是最接近问题的。

我找到了不禁用 SELinux 的解决方案。

我是 SELinux 的新手,但我通过一些 SELinux 配置解决了这个问题,将来可能会有人感兴趣。

上下文:

  • Centos 7

  • SNMP /etc/snmp/snmpd.conf 配置文件的内容:


#       sec.name    source      community
com2sec myuser default public
# groupName securityModel securityName
group mygroup v2c pad
# name incl/excl subtree mask(optional)
view systemview included .1.3.6.1.4
view systemview included .1.3.6.1.4.1
view systemview included .1.3.6.1.4.1.1234
view systemview included .1.3.6.1.4.1.1234.1
# group context sec.model sec.level prefix read write notif
access mygroup "" any noauth exact systemview none none
perl do "/appli/snmp_scripts/agent.pl"

  • SNMAP 代理 agent.pl :

#!/usr/bin/perl

use NetSNMP::agent (':all');
use NetSNMP::ASN qw(ASN_OCTET_STR ASN_INTEGER);

sub handler {
my ($handler, $registration_info, $request_info, $requests) = @_;
my $request;

for($request = $requests; $request; $request = $request->next()) {
my $oid = $request->getOID();
if ($request_info->getMode() == MODE_GET) {
if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.1581.1.6.2.1.5")) {
$request->setValue(ASN_OCTET_STR,`/path/to/myscript`);
}
}
}
}

my $AGENT_OID = ".1.3.6.1.4.1.1234";
$agent->register("MYAGENT", ".1.3.6.1.4.1.1234",
\&handler);

解决方案:

当我使用 sudo systemctl status snmpd 寻找执行的痕迹时,显示了一些 curl 痕迹:

snmpd: curl: (7) Failed to connect to 127.0.0.1:8081 Permission denied

但是,服务器在此端口运行良好,并且在 SNMP 外部执行的脚本运行良好。

审计日志中生成了 SELinux 错误:

$> sudo grep snmp /var/log/audit/audit.log | audit2allow -w -a
type=AVC msg=audit(1528188940.802:2025): avc: denied { name_connect } for pid=26809 comm="curl" dest=8081 scontext=system_u:system_r:snmpd_t:s0 tcontext=system_u:object_r:transproxy_port_t:s0 tclass=tcp_socket
Was caused by:
Missing type enforcement (TE) allow rule.

You can use audit2allow to generate a loadable module to allow this access.

$> sudo grep snmp /var/log/audit/audit.log | audit2allow -M mysnmpmodule

******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i snmpdcanopensocket.pp

按照 audit2allow comm 指示的步骤将新创建的 snmp_t 模块应用于 SELinux。 audit2allow -M 命令在当前目录中生成了两个文件 snmpdcanopensocket.pp snmpdcanopensocket.te。SELinux 需要 .pp 文件来重新映射它的安全规则。

$> semodule -i snmpdcanopensocket.pp

使用 sudo systemctl restart snmp 重启 SNMP 服务

现在,由 SNMP 执行的脚本中的 curl 表现良好,不会以 (7) 错误代码退出。

关于linux - Cacti 通过自定义脚本扩展 snmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com