Hello-Linux / elastalert_wechat_plugin

一个更加人性化,智能化,简单化的elastalert微信报警插件!
https://github.com/Hello-Linux/elastalert_wechat_plugin
Apache License 2.0
77 stars 17 forks source link

python3.6 企业微信无法报警,python2都快不支持了 #7

Open yezhiyuan opened 5 years ago

yezhiyuan commented 5 years ago

InsecureRequestWarning, ERROR:root:Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1440, in alert return self.send_alert(matches, rule, alert_time=alert_time, retried=retried) File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1529, in send_alert alert.alert(matches) File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 63, in alert self.senddata(body) File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 130, in senddata response = requests.post(send_url, data=json.dumps(payload, ensure_ascii=False), headers=headers,verify=False) File "/usr/lib/python3.6/json/init.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'bytes' is not JSON serializable

yezhiyuan commented 5 years ago

我安装的Elastalert 用的python3.6

Hello-Linux commented 5 years ago

@yezhiyuan 目前你先别用python3,可能有函数以及类的不兼容,后期我会尽快过渡到python3.

yezhiyuan commented 5 years ago

但现在安装的elastlaert python2默认不行,估计要安装你之前的分支才行;

------------------ 原始邮件 ------------------ 发件人: "Hello-Linux"<notifications@github.com>; 发送时间: 2019年11月6日(星期三) 晚上7:59 收件人: "Hello-Linux/elastalert_wechat_plugin"<elastalert_wechat_plugin@noreply.github.com>; 抄送: "叶志远"<382111883@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Hello-Linux/elastalert_wechat_plugin] python3.6 企业微信无法报警,python2都快不支持了 (#7)

目前你别别用python3,可能有函数以及类的不兼容,后期我会尽快过渡到python3.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Hello-Linux commented 5 years ago

启动后的日志你观察了么,有没有触发你的规则?再就是python2.7目前没有问题我自己的网站目前一直在使用中。

yezhiyuan commented 5 years ago

启动后一段时间会报这个错误;   InsecureRequestWarning, ERROR:root:Traceback (most recent call last):   File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1440, in alert     return self.send_alert(matches, rule, alert_time=alert_time, retried=retried)   File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1529, in send_alert     alert.alert(matches)   File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 63, in alert     self.senddata(body)   File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 130, in senddata     response = requests.post(send_url, data=json.dumps(payload, ensure_ascii=False), headers=headers,verify=False)   File "/usr/lib/python3.6/json/init.py", line 238, in dumps     **kw).encode(obj)   File "/usr/lib/python3.6/json/encoder.py", line 199, in encode     chunks = self.iterencode(o, _one_shot=True)   File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode     return _iterencode(o, 0)   File "/usr/lib/python3.6/json/encoder.py", line 180, in default     o.class.name) TypeError: Object of type 'bytes' is not JSON serializable

然后等一会就会禁用我的报警规则了,有时候启动过很久才出现上面的错误,但是收不到告警, 应该语法哪里有问题吧,毕竟我是用python3跑的

我安装的Elastalert 用的pip3;默认python环境3.6,

------------------ 原始邮件 ------------------ 发件人: "Hello-Linux"<notifications@github.com>; 发送时间: 2019年11月6日(星期三) 晚上8:07 收件人: "Hello-Linux/elastalert_wechat_plugin"<elastalert_wechat_plugin@noreply.github.com>; 抄送: "叶志远"<382111883@qq.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [Hello-Linux/elastalert_wechat_plugin] python3.6 企业微信无法报警,python2都快不支持了 (#7)

启动后的日志你观察了么,有没有触发你的规则?再就是python2.7目前没有问题我自己的网站目前一直在使用中。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

yezhiyuan commented 5 years ago

规则肯定触发了,到alert sent 一直都是0,我用邮件是正常的收到告警

------------------ 原始邮件 ------------------ 发件人: "Hello-Linux"<notifications@github.com>; 发送时间: 2019年11月6日(星期三) 晚上8:07 收件人: "Hello-Linux/elastalert_wechat_plugin"<elastalert_wechat_plugin@noreply.github.com>; 抄送: "叶志远"<382111883@qq.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [Hello-Linux/elastalert_wechat_plugin] python3.6 企业微信无法报警,python2都快不支持了 (#7)

启动后的日志你观察了么,有没有触发你的规则?再就是python2.7目前没有问题我自己的网站目前一直在使用中。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Hello-Linux commented 5 years ago

好的,谢谢你的反馈,目前我使用的elastlaert 是之前的旧版本我在测试环境试试最新版的,后面我观察一下。

Hello-Linux commented 5 years ago

@yezhiyuan 刚测试完了,是可以发送的,你这边检测一下你的微信配置有没有问题。

yezhiyuan commented 5 years ago

嗯;我的老是报这个错误,启动的时候

INFO:elastalert:Disabled rules are: [] INFO:elastalert:Sleeping for 59.999921 seconds INFO:elastalert:Queried rule es_rule from 2019-11-06 19:48 CST to 2019-11-06 20:03 CST: 85 / 85 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 20:03 CST to 2019-11-06 20:18 CST: 148 / 148 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 20:18 CST to 2019-11-06 20:33 CST: 116 / 116 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 20:33 CST to 2019-11-06 20:48 CST: 159 / 159 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 20:48 CST to 2019-11-06 21:03 CST: 135 / 135 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 21:03 CST to 2019-11-06 21:18 CST: 406 / 406 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 21:18 CST to 2019-11-06 21:33 CST: 483 / 483 hits INFO:elastalert:Queried rule es_rule from 2019-11-06 21:33 CST to 2019-11-06 21:47 CST: 103 / 103 hits /usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py:1004: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning, ERROR:root:Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1440, in alert return self.send_alert(matches, rule, alert_time=alert_time, retried=retried) File "/usr/local/lib/python3.6/dist-packages/elastalert/elastalert.py", line 1529, in send_alert alert.alert(matches) File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 63, in alert self.senddata(body) File "/root/elastalert/elastalert_wechat_plugin/es_rules/elastalert_modules/wechat_qiye_alert.py", line 130, in senddata response = requests.post(send_url, data=json.dumps(payload, ensure_ascii=False), headers=headers,verify=False) File "/usr/lib/python3.6/json/init.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'bytes' is not JSON serializable

ERROR:root:Uncaught exception running rule es_rule: Object of type 'bytes' is not JSON serializable

测试语法好像没什么问题;

root@elk:~/elastalert/elastalert_wechat_plugin/es_rules# elastalert-test-rule --config config.yaml wechart.yaml INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent. To send them but remain verbose, use --verbose instead. Didn't get any results. INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent. To send them but remain verbose, use --verbose instead. 1 rules loaded INFO:apscheduler.scheduler:Adding job tentatively -- it will be properly scheduled when the scheduler starts INFO:elastalert:Queried rule es_rule from 2019-11-06 21:48 CST to 2019-11-06 21:49 CST: 1 / 1 hits

Would have written the following documents to writeback index (default is elastalert_status):

elastalert_status - {'rule_name': 'es_rule', 'endtime': datetime.datetime(2019, 11, 6, 13, 49, 29, 952623, tzinfo=tzutc()), 'starttime': datetime.datetime(2019, 11, 6, 13, 48, 29, 352623, tzinfo=tzutc()), 'matches': 0, 'hits': 1, '@timestamp': datetime.datetime(2019, 11, 6, 13, 49, 30, 41775, tzinfo=tzutc()), 'time_taken': 0.019246578216552734}

yezhiyuan commented 5 years ago

希望有空了帮忙看看上面的报错,应该就是那个问题导致无法报警

Hello-Linux commented 5 years ago

这个报错是python3.6的,你使用python2.7有问题么

yezhiyuan commented 5 years ago

root@elk:~/elastalert/elastalert_wechat_plugin/es_rules# elastalert-test-rule --config config.yaml wechart.yaml Traceback (most recent call last): File "/usr/local/bin/elastalert-test-rule", line 11, in load_entry_point('elastalert==0.2.1', 'console_scripts', 'elastalert-test-rule')() File "/usr/local/lib/python2.7/dist-packages/pkg_resources/init.py", line 489, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/local/lib/python2.7/dist-packages/pkg_resources/init.py", line 2852, in load_entry_point return ep.load() File "/usr/local/lib/python2.7/dist-packages/pkg_resources/init.py", line 2443, in load return self.resolve() File "/usr/local/lib/python2.7/dist-packages/pkg_resources/init.py", line 2449, in resolve module = import(self.module_name, fromlist=['name'], level=0) File "/usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg/elastalert/test_rule.py", line 61 print('Invalid filter provided:', str(ea), file=sys.stderr) ^ SyntaxError: invalid syntax 语法测试报错

yezhiyuan commented 5 years ago

用python3.6语法测试没问题

yezhiyuan commented 5 years ago

root@elk:~/elastalert/elastalert_wechat_plugin/es_rules# python -m elastalert.elastalert --verbose --rule wechart.yaml Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main "main", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg/elastalert/elastalert.py", line 29, in from . import kibana File "/usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg/elastalert/kibana.py", line 4, in import urllib.error ImportError: No module named error

yezhiyuan commented 5 years ago

用python2.7安装的时候已经报错了; byte-compiling build/bdist.linux-x86_64/egg/tests/create_index_test.py to create_index_test.pyc byte-compiling build/bdist.linux-x86_64/egg/tests/auth_test.py to auth_test.pyc creating build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying elastalert.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO zip_safe flag not set; analyzing archive contents... elastalert.create_index: module references file elastalert.loaders: module references file creating dist creating 'dist/elastalert-0.2.1-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it removing 'build/bdist.linux-x86_64/egg' (and everything under it) Processing elastalert-0.2.1-py2.7.egg removing '/usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg' (and everything under it) creating /usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg Extracting elastalert-0.2.1-py2.7.egg to /usr/local/lib/python2.7/dist-packages File "/usr/local/lib/python2.7/dist-packages/elastalert-0.2.1-py2.7.egg/elastalert/test_rule.py", line 61 print('Invalid filter provided:', str(ea), file=sys.stderr) ^ SyntaxError: invalid syntax

elastalert 0.2.1 is already the active version in easy-install.pth Installing elastalert-create-index script to /usr/local/bin Installing elastalert script to /usr/local/bin Installing elastalert-rule-from-kibana script to /usr/local/bin

requests和requests-dev里面的依赖都正常安装了;

yezhiyuan commented 5 years ago

应该是要python3才能安装上;

yezhiyuan commented 5 years ago

默认安装的版本是0.2.1,然后你的手册要求必须python3; Running ElastAlert for the First Time

Requirements Elasticsearch ISO8601 or Unix timestamped data Python 3.6 pip, see requirements.txt Packages on Ubuntu 14.x: python-pip python-dev libffi-dev libssl-dev

yezhiyuan commented 5 years ago

我现在的主要问题就是使用2.7安装报警器直接报错,用3.6可以装上,但是你的微信报警是用python2.7开发的,导致很多不兼容;多次打扰,真的是不好意思;

Hello-Linux commented 5 years ago

@yezhiyuan 嗯嗯 后面我更新一下代码版本

yezhiyuan commented 4 years ago

好的,非常感谢!

Hello-Linux commented 4 years ago

@yezhiyuan 最近在忙其他事情,我尽快吧。

yezhiyuan commented 4 years ago

谢谢你哈!辛苦了!你有空了就弄一下,没空就先放着!真的没事。

station19 commented 4 years ago

我也遇到这个问题, python3 报 TypeError: Object of type bytes is not JSON serializable

station19 commented 4 years ago

已经搞定了, 支持最新版的 elastalert0.21, 支持elasticsearch7.x https://github.com/station19/elastalert-wechat-plugin/blob/master/elastalert_modules/wechat_qiye_alert_new.py

cimengyuanjun commented 4 years ago

我也遇到一样的问题了,小白完全不知道该怎么解决。。。

leqii-com commented 2 years ago

@station19 没有解决。你的代码我试了,一样的问题。一毛一样。

leqii-com commented 2 years ago

@yezhiyuan 我遇到跟你一样的问题,email是可以发送的,就是企业微信发不了。我安装的anaconda3,因为pip版本太低不让安装elastalert,只能安装python3以上了,然而这个企业微信告警的py是python2的写法,醉了。

程序运行报错: 1 rules loaded /root/anaconda3/lib/python3.8/site-packages/APScheduler-3.8.1-py3.8.egg/apscheduler/util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html if obj.zone == 'local': /root/anaconda3/lib/python3.8/site-packages/APScheduler-3.8.1-py3.8.egg/apscheduler/util.py:166: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html return tz.localize(datetime_, is_dst=None) INFO:elastalert:Starting up /root/anaconda3/lib/python3.8/site-packages/APScheduler-3.8.1-py3.8.egg/apscheduler/triggers/interval.py:66: PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html return self.timezone.normalize(next_fire_time) INFO:elastalert:Disabled rules are: [] INFO:elastalert:Sleeping for 59.999858 seconds INFO:elastalert:Queried rule frequency_rule from 2021-11-11 10:03 CST to 2021-11-11 10:18 CST: 428 / 428 hits INFO:elastalert:Queried rule frequency_rule from 2021-11-11 10:18 CST to 2021-11-11 10:28 CST: 198 / 198 hits /root/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py:1013: InsecureRequestWarning: Unverified HTTPS request is being made to host 'qyapi.weixin.qq.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings warnings.warn( ERROR:root:Traceback (most recent call last): File "/root/anaconda3/lib/python3.8/site-packages/elastalert-0.2.4-py3.8.egg/elastalert/elastalert.py", line 1452, in alert return self.send_alert(matches, rule, alert_time=alert_time, retried=retried) File "/root/anaconda3/lib/python3.8/site-packages/elastalert-0.2.4-py3.8.egg/elastalert/elastalert.py", line 1546, in send_alert alert.alert(matches) File "/root/anaconda3/lib/python3.8/site-packages/elastalert-0.2.4-py3.8.egg/elastalert_modules/wechat_qiye_alert.py", line 57, in alert self.senddata(body) File "/root/anaconda3/lib/python3.8/site-packages/elastalert-0.2.4-py3.8.egg/elastalert_modules/wechat_qiye_alert.py", line 124, in senddata response = requests.post(send_url, data=json.dumps(payload, ensure_ascii=False), headers=headers,verify=False) File "/root/anaconda3/lib/python3.8/json/init.py", line 234, in dumps return cls( File "/root/anaconda3/lib/python3.8/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/root/anaconda3/lib/python3.8/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/root/anaconda3/lib/python3.8/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type bytes is not JSON serializable

ERROR:root:Uncaught exception running rule frequency_rule: Object of type bytes is not JSON serializable

最后一条信息:

INFO:elastalert:Ignoring match for silenced rule frequency_rule INFO:elastalert:Ran frequency_rule from 2021-11-11 10:28 CST to 2021-11-11 13:30 CST: 34 query hits (0 already seen), 906 matches, 0 alerts sent INFO:elastalert:Background configuration change check run at 2021-11-11 13:31 CST INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-11-11 13:31 CST INFO:elastalert:Disabled rules are: ['frequency_rule'] INFO:elastalert:Sleeping for 59.999802 seconds

请问楼主解决没?版主有方法?我现在是python3.8环境

@Hello-Linux