from kubernetes import client, config, watch
from elasticsearch import Elasticsearch
import arrow
import sys
import requests
import json
dingding_webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
hosts = [
'172.16.21.39:9200',
'172.16.21.40:9200',
'172.16.21.41:9200'
]
def send_text(content):
data = {
"msgtype": "text",
"text": {
"content": content
}
}
requests.post(url=dingding_webhook, json=data)
Now = arrow.Now('Asia/Shanghai')
index = "kube-events"
index_today = "{}.{}".format(index, Now.format("YYYY.MM.DD"))
cluster_name = sys.argv[1]
kube_config = sys.argv[2]
with Elasticsearch(hosts=hosts, timeout=120) as es:
if not es.indices.exists(index=index_today):
es.indices.create(index=index_today)
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(config_file=kube_config)
v1 = client.CoreV1Api()
w = watch.Watch()
try:
for event in w.stream(v1.list_event_for_all_namespaces):
doc = {
"cluster_name": cluster_name,
"time_iso8601": arrow.Arrow.fromdatetime(event['object'].Metadata.creation_timestamp).isoformat(),
"namespace": event['object'].Metadata.namespace,
"type": event['object'].type,
"reason": event['object'].reason,
"message": event['object'].message
}
# print(json.dumps(doc))
# if doc["type"] == "Warning":
# send_text('[ {} ]- {}'.format(cluster_name, doc["message"]))
if es.exists(index=index_today, doc_type=index, id=event['object'].Metadata.uid):
continue
es.create(index=index_today, doc_type=index, body=doc, id=event['object'].Metadata.uid)
except Exception as e:
pass
w.stop()
使用 supervisord 将程序后台运行。
[program:ali-14]
command = /usr/local/bin/python3.6 /ops/scripts/kube_events.py ali-14 /ops/k8s_config/ali-14
autorestart = true
redirect_stderr = true
stdout_logfile = /ops/logs/ali-14.log
stopasgroup = true
[program:m7]
command = /usr/local/bin/python3.9 /ops/scripts/kube_events.py m7 /ops/k8s_config/m7
autorestart = true
redirect_stderr = true
stdout_logfile = /ops/logs/m7.log
stopasgroup = true
[program:cm]
command = /usr/local/bin/python3.9 /ops/scripts/kube_events.py cm /ops/k8s_config/cm
autorestart = true
redirect_stderr = true
stdout_logfile = /ops/logs/cm.log
stopasgroup = true
[program:meteo]
command = /usr/local/bin/python3.6 /ops/scripts/kube_events.py meteo /ops/k8s_config/meteo
autorestart = true
redirect_stderr = true
stdout_logfile = /ops/logs/meteo.log
stopasgroup = true
写入 ES 之后就可以通过 kibana 查看了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。