No module named modules.my_rules
Hi !
First of all, thank you for Elastalert. It does work wonderfully (at least with default rules and alerts).
I've been trying to write custom rules and alerts, using the documentation. However even while following religiously the documentation, when trying my rules or alerts, I get the No module named modules.my_rules error.
Here is my modules folder :
modules
├── __init__.py
├── my_alerts.py
└── my_rules.py
My rules folder :
rules
└── rule.yaml
Attached are the sample rule I'm using, and the two .py files. Those are pretty basics, my_rules.py is basically taken from the documentation.
my_alerts.py.txt my_rules.py.txt rule.yaml.txt
I can import them perfectly fine from the Python REPL while being at /opt/elastalert. But when running elastalert either from /opt/elastalert and running elastalert, with elastalert-test-rule or via supervisor, the import fails.
Would anyone have any idea of what might be causing this ?
Thanks!
Hmm, your code looks fine. Maybe this is a dumb question, but it's not a .txt file is it? Does the alert module work if you remove my_rules type?
Hi,
As you can see in my tree command, those are .py files.
My rule does not make use of the my_rules.py file. It uses a default rule with the alerter in my_alerts.py.
I do wonder why it is not working, seeing I followed meticulously the tutorial examples.
By the way this is on Ubuntu 14.04 with Python 2.7.6, and what I think of as a pretty recent version of elastalert (September 10th).
Edit : I just tried fetching master and running setup.py again. No luck, still can't import :
Traceback (most recent call last):
File "/usr/local/bin/elastalert-test-rule", line 11, in <module>
load_entry_point('elastalert==0.0.96', 'console_scripts', 'elastalert-test-rule')()
File "/usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/elastalert/test_rule.py", line 317, in main
test_instance.run_rule_test()
File "/usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/elastalert/test_rule.py", line 312, in run_rule_test
self.run_elastalert(rule_yaml, conf, args)
File "/usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/elastalert/test_rule.py", line 180, in run_elastalert
load_modules(rule)
File "/usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/elastalert/config.py", line 314, in load_modules
rule['alert'] = load_alerts(rule, alert_field=rule['alert'])
File "/usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/elastalert/config.py", line 371, in load_alerts
raise EAException('Error initiating alert %s: %s' % (rule['alert'], e))
elastalert.util.EAException: Error initiating alert ['email', 'victorops', 'modules.my_alerts.osTicketAlerter']: Could not import module modules.my_alerts.osTicketAlerter: No module named modules.my_alerts
To confirm, just running python -m modules.my_alerts works?
Well, there is no particular output. But it does work :
elastalert@wks:/opt/elastalert$ python -m modules.my_alerts
elastalert@wks:/opt/elastalert$
OK so I think I figured it out.
When installing Elastalert, I git clone'd the repo, and python setup.py install.
However when testing and running my modules with Elastalert, I was using
$ elastalert-test-rule
$ which elastalert-test-rule
/usr/local/bin/elastalert-test-rule
Furthermore when invoking components such as config.py and test_rule, we see in the log that Elastalert invokes those of /usr/local/lib/python2.7/dist-packages/elastalert-0.0.96-py2.7.egg/
Running in my folder (/opt/elastalert) python -m elastalert.test_rule or python -m elastalert.elastalert does seem to do the trick.
I thought I had read throughly the documentation, and therefore think there may be some things left unclear.
Now I just have to debug my alerting rule.
Thanks again for the help and for the project !
@Qmando
Thank you Qmando, As per your suggestion i have written any type rule file by using regular expression. Inside rule file we are using customized alert for which we have writen python script when i run the command it is giving error message: command is /usr/bin/elastalert --config /etc/elastalert/conf.d/config.yaml --verbose
below us the rule file:
num_events: 5
timeframe:
hours: 72
filter:
- query:
query_string:
query: "sp-stackTrace: *org.apache.hadoop.hbase* OR sp-message: *org.apache.hadoop.hbase*"
max_query_size: 1000
alert:
- "new_modules.compass_alerter.CompassAlerter"
alert_subject: "HBase Related Error(s) for CDAP running in Titan {0}"
command that i am ruuning is: /usr/bin/elastalert --config /etc/elastalert/conf.d/config.yaml --verbose
Error details:
Traceback (most recent call last):
File "/usr/bin/elastalert", line 11, in
I am not sure where to import the file that shows in error message could you please suggest me on this.
Try going to the directory where new_modules folder is, then running ElastAlert via python -m elastalert.elastalert. I think one common issue is that when you install elastalert and run it via /usr/bin/elastalert, it changes your working directory and it can't import from right place. Adding the current directory to PYTHONPATH may also help.
Thank you so much @Qmando python command works for me.