.net重启zabbix服务

本文介绍了如何在.NET环境下修改Zabbix_agentd的主动式监控配置文件,通过正则表达式更新ServerActive参数。接着,文章演示了如何使用.NET库停止和启动Zabbix_agentd服务,确保配置变更生效。需要注意的是,由于权限问题,程序需在具有相应权限的用户下运行,如通过IIS部署并设置应用池。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用zabbix监控位于局域网中的主机,由于被监控主机没有公网ip,所以使用主动式zabbix_agentd进行监控。

主动式监控(zabbix_agentd主动向zabbix_server发送数据)配置文件

#### 日志文件位置
LogFile=c:\zabbix_agentd\zabbix_agentd.log

#### 禁用被动模式
StartAgents=0

#### 主动上传数据ip地址
ServerActive=210.73.27.13

#### 主机名(对应zabbix_server中的配置)
Hostname=zhezhao
#### 每隔1分钟,向zabbix_server确认一次需要收集的数据
RefreshActiveChecks=60

现在有个需求,就是zabbix_server的域名(或者ip地址)在将来有可能会发生变化,要求可以通过web页面修改zabbix_agent配置文件中的ServerActive参数,并重启服务。

我们先读取zabbix_agentd.conf配置文件,通过正则表达式替换ServerActive参数。

我们可以通过添加.net framework库中的System.ServiceProcess引用,来停止和开启zabbix_agentd服务。

替换完成配置文件中的ServerActive参数之后,重启zabbix_agentd服务,新的配置文件就会生效。

using System.ServiceProcess;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace zabbix.Controllers
{
    public class IndexController : Controller
    {
        private ServiceController service;
        public IndexController()
        {
            this.service = new ServiceController("Zabbix Agent");
        }
        // GET: Index
        public ActionResult Index()
        {
            //读取zabbi_agent服务状态
            string state = this.service.Status.ToString();
            ViewBag.state = state;
            return View();
        }
        public string Status()
        {
            string state = this.service.Status.ToString();
            return state;
        }
        public string Stop()
        {
            this.service.Stop();
            this.service.WaitForStatus(ServiceControllerStatus.Stopped);
            return "操作成功";
        }
        public string Start()
        {
            this.service.Start();
            //等待服务到达运行状态
            this.service.WaitForStatus(ServiceControllerStatus.Running);
            return "操作成功";
        }
        public string ReStart()
        {
            this.Stop();
            this.Start();
            return "操作成功";
        }
        private string ReConfig()
        {
            string newIp;
            //检查ip参数
            if (Request["ip"].Trim() == "") {
                return "ip参数不正确";
            }

            newIp = Request["ip"].Trim();
            newIp = newIp+"\n";
            string config =            System.IO.File.ReadAllText(@"C:\zabbix_agentd\conf\zabbix_agentd.conf");
            Regex r = new Regex("ServerActive=(.*)\n");
            if (r.IsMatch(config))
            {
                MatchCollection mc = r.Matches(config);
                string val =  mc[0].Value;
                string[] arr = val.Split('=');
                string oldIp = arr[1];
                string newConfig = config.Replace(oldIp, newIp);
        System.IO.File.WriteAllText(@"C:\zabbix_agentd\conf\zabbix_agentd.conf.backup",config);
               System.IO.File.WriteAllText(@"C:\zabbix_agentd\conf\zabbix_agentd.conf", newConfig);
                //检查服务是否在运行
                if (this.Status() != "Running")
                {
                    this.Start();
                }else
                {
                    this.ReStart();
                }
                return "操作成功";
            }
            else
            {
                return "配置文件写入失败";
            }
        }
    }
}

使用说明

直接在vs中调试程序会报错,因为默认用户的权限是不能重启zabbix服务的。

我们需要发布到IIS服务器,然后为它创建一个应用池,该应用池的“标识”设置为一个有权限的用户即可。

这里写图片描述

地址说明
IndexController/Status查询Zabbix Agent服务运行状态
IndexController/Stop停止Zabbix Agent服务
IndexController/Start开启Zabbix Agent服务
IndexController/Restart重启Zabbix Agent服务
IndexController/ReConfig接收ip参数,修改zabbix配置文件,并重启服务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值