使用Zabbix-agent2自定义插件获取https证书过期时间

微信公众号:运维开发故事,作者:wanger

需求

对经常维护网站的人来讲,是要经常跟https的证书打交道的,一般https证书的有效期在一年的时间,证书一旦过期,对公司的损失是非常大的,去年网易邮箱因为https证书忘记续期,导致大量用户无法正常使用邮箱就是个很好的反例,我们也不能什么时候想起来才去手动查一下,这样也不现实,最好的方法是把过期时间监控起来,距离一定期限自动发送通知,可以使用zabbix或者Prometheus的ssl_exporter来进行监控,在zabbix4.4版本以前可以使用自定义脚本的方式,在4.4之后出现了zabbix-agent2,除了官方自带的插件也可以通过自定义插件的方式来满足我们的监控需求,那么接下来就给大家介绍如何使用zabbix-agent2自定义插件来实现获取https证书过期时间的需求。

zabbix-agent2自定义https_expire插件

之前问们已经介绍过如何使用自定义插件来实现对mqtt的监控,原文地址https://mp.weixin.qq.com/s/4e6WnFyjXZYnXphx2erkGw,只不过当时使用的Watcher接口来将新数据主动push给server端,这次将通过实现Exporter接口来采集数据,这里再次提供下官方文档https://www.zabbix.com/documentation/current/manual/config/items/plugins和Zabbix认证专家米宏翻译的官方博文https://mp.weixin.qq.com/s/7JLuVdZYX719pgGSP3xc9w
这里我再介绍一下自定义插件的一些标准规范
1. 插件必须导入zabbix.com/pkg/plugin软件包。

import "zabbix.com/pkg/plugin"

1. 插件必须定义结构并嵌入该plugin.Base结构。

type Plugin struct {
    plugin.Base
}
var impl Plugin

1. 一个插件必须实现一个或多个插件接口。

func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
    if len(params) > 0 {
        p.Debugf("received %d parameters while expected none", len(params))
        return nil, errors.New("Too many parameters")
    }
    return time.Now().Format(time.RFC3339)
}

1. 插件必须在初始化期间注册自己。

func init() {
    plugin.RegisterMetrics(&impl, "Time", "system.time", "Returns time string in RFC 3999 format.")
}

ssl_expire代码不多,插件由尼古拉·拖拉基斯基·王二编写,完整代码可以去github查看

func (p *Plugin) Configure(global *plugin.GlobalOptions, options interface{}) {
	if err = conf.Unmarshal(options, &p.options); err != nil {
		p.Errf("cannot unmarshal configuration options: %s", err)
	}

	if p.options.Timeout == 0 {
		p.options.Timeout = global.Timeout
	}
	p.client = newClient(p.options.Timeout)

}

func (p *Plugin) Validate(options interface{}) error {

	return conf.Unmarshal(options, &opts)
}

func checkParamnums(params []string) error {
	if len(params) > paramnums {
		err:=errors.New("Too many parameters.")
		return zbxerr.ErrorTooFewParameters.Wrap(err)
	} else if len(params) ==0 {
		err:=errors.New("Missing URL parameters.")
		return zbxerr.ErrorTooFewParameters.Wrap(err)
	}
	return nil
}

func checkParams(params []string) (string, error) {
	if strings.HasPrefix(params[0], "http://") {
		errorsting:=fmt.Sprintf("Target is using http scheme: %s", params[0])
		err:=errors.New(errorsting)
		return "",zbxerr.ErrorInvalidParams.Wrap(err)
	}

	if !strings.HasPrefix(params[0], "https://") {
		params[0] = "https://" + params[0]
	}
	return string(params[0]),nil
}
func (cli *client) Query(url string) (int64, error) {
	resp, err := cli.client.Get(url)
	if err != nil {
		impl.Debugf("cannot fetch data: %s", err)
		err:=errors.New("cannot fetch data")
		return 0, zbxerr.ErrorCannotFetchData.Wrap(err)
	}
	defer resp.Body.Close()
	certInfo:=resp.TLS.PeerCertificates[0]
	expiredays:=(certInfo.NotAfter.Unix()-time.Now().Unix())/60/60/24
	return expiredays,nil
}

// Export implements the Exporter interface.
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (interface{}, error) {
	if err = checkParamnums(params); err != nil {
		return nil, err
	}
	urls,err:= checkParams(params)
	if err!= nil {
		return nil,err
	}
	body, err := p.client.Query(urls)
	if err!=nil{
		return nil, err
	}
	return body,nil

}
func init() {
	plugin.RegisterMetrics(&impl, pluginName,
		"https_expire", "Returns the number of days between the HTTPS certificate expiration time and the current date.")
}

下载zabbix agent2源码并将自定义插件编译

yum install golang
git clone https://git.zabbix.com/scm/zbx/zabbix.git --depth 1 zabbix-agent2
cd zabbix-agent2
git submodule add https://github.com/cxf210/ssl_expire.git src/go/plugins/https_expire

导入https_expire插件

vi src/go/plugins/plugins_linux.go

添加最后一行

        _ "zabbix.com/plugins/ceph"
        _ "zabbix.com/plugins/docker"
        _ "zabbix.com/plugins/kernel"
        _ "zabbix.com/plugins/log"
        _ "zabbix.com/plugins/memcached"
        _ "zabbix.com/plugins/modbus"
        _ "zabbix.com/plugins/mqtt"
        _ "zabbix.com/plugins/mysql"
        _ "zabbix.com/plugins/net/netif"
        _ "zabbix.com/plugins/net/tcp"
        ...
        _ "zabbix.com/plugins/https_expire"

编译安装zabbix agent2

yum install automake autoconf pcre* -y
./bootstrap.sh 
pushd . 
cd src/go/ 
go mod vendor 
popd 
./configure --enable-agent2 --enable-static 
make install

编辑配置文件

这里我调整了日志级别,方便前台调试
可选参数
Plugins.Https_expire.Timeout = 5

egrep -v "^$|^#" conf/zabbix_agent2.conf  
LogType=console
LogFile=/tmp/zabbix_agent2.log
DebugLevel=4
Server=172.17.0.5
Plugins.Https_expire.Timeout=5
Hostname=node2
ControlSocket=/tmp/agent.sock

启动Zabbix_agent2

cd /root/zabbix_agent/src/go/bin
zabbix_agent2 -c conf/zabbix_agent2.conf

![image.png](https://img-blog.csdnimg.cn/img_convert/01e581803e6800e564773504acd5a50d.png#align=left&display=inline&height=81&margin=[object Object]&name=image.png&originHeight=161&originWidth=1060&size=30987&status=done&style=none&width=530)

Zabbix创建监控项

键值示例如下

https_expire["www.xyzabbix.cn"]

https_expire["https://www.xyzabbix.cn"]

![image.png](https://img-blog.csdnimg.cn/img_convert/d1fe529aa9654569c15a7e152c271045.png#align=left&display=inline&height=336&margin=[object Object]&name=image.png&originHeight=672&originWidth=930&size=46093&status=done&style=none&width=465)
查看最新数据,这个证书还有四十天过期
![image.png](https://img-blog.csdnimg.cn/img_convert/532389a54e779e6c31ee9a228b2d950a.png#align=left&display=inline&height=80&margin=[object Object]&name=image.png&originHeight=159&originWidth=1625&size=15012&status=done&style=none&width=812.5)
我是用的阿里云ssl证书,可以看到确实离过期时间还有四十天,今天是2021.3.7
![image.png](https://img-blog.csdnimg.cn/img_convert/a9e0059914c661a2288c57a573f193b2.png#align=left&display=inline&height=118&margin=[object Object]&name=image.png&originHeight=236&originWidth=1497&size=21620&status=done&style=none&width=748.5)
可以创建一个触发器,在还有一个月的时候发送报警通知
![image.png](https://img-blog.csdnimg.cn/img_convert/7921a39a29c3807bfbde3372e13b2f02.png#align=left&display=inline&height=302&margin=[object Object]&name=image.png&originHeight=603&originWidth=949&size=40242&status=done&style=none&width=474.5)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值