对常常维护网站的人来讲,要常常跟https的证书打交道。通常https证书的有效期是一年,证书一旦过时,公司的损失会很是大。去年网易邮箱由于https证书忘记续期,致使大量用户没法正常使用邮箱就是个典型案例。何时想起来才去手动查一下也不现实,最好的方法是把过时时间监控起来,距离必定期限自动发送通知。
node
可使用Zabbix或者Prometheus的ssl_exporter来进行监控,在Zabbix4.4版本之前可使用自定义脚本的方式,在Zabbix4.4以后出现了zabbix-agent2,除了官方自带的插件也能够经过自定义插件的方式来知足咱们的监控需求。本文介绍如何使用zabbix-agent2自定义插件来实现获取https证书过时时间的需求。mysql
以前介绍过如何使用自定义插件来实现对mqtt的监控,只不过当时使用的Watcher接口来将新数据主动push给server端,此次将经过实现Exporter接口来采集数据,再次提供官方文档和Zabbix认证专家米宏翻译的官方博文。
这里我再介绍一下自定义插件的一些标准规范 linux
import "zabbix.com/pkg/plugin"
type Plugin struct { plugin.Base } var impl Plugin
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) }
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.") }
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
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"
yum install automake autoconf pcre* -y ./bootstrap.sh pushd . cd src/go/ go mod vendor popd ./configure --enable-agent2 --enable-static make install
这里我调整了日志级别,方便前台调试 可选参数 git
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
cd /root/zabbix_agent/src/go/bin zabbix_agent2 -c conf/zabbix_agent2.conf
键值示例以下github
https_expire["www.xyzabbix.cn"]
或golang
https_expire["https://www.xyzabbix.cn"]
查看最新数据,这个证书还有四十天过时sql
我是用的阿里云ssl证书,能够看到确实离过时时间还有四十天,今天是2021.3.7docker
能够建立一个触发器,在还有一个月的时候发送报警通知。bootstrap