elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

No module named modules.my_rules

Open mantoine96 opened this issue 10 years ago • 8 comments

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!

mantoine96 avatar Sep 20 '16 14:09 mantoine96

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?

Qmando avatar Sep 20 '16 17:09 Qmando

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

mantoine96 avatar Sep 21 '16 07:09 mantoine96

To confirm, just running python -m modules.my_alerts works?

Qmando avatar Sep 21 '16 16:09 Qmando

Well, there is no particular output. But it does work :

elastalert@wks:/opt/elastalert$ python -m modules.my_alerts
elastalert@wks:/opt/elastalert$

mantoine96 avatar Sep 22 '16 09:09 mantoine96

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 !

mantoine96 avatar Sep 23 '16 06:09 mantoine96

@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 sys.exit(main()) File "/usr/lib/python2.7/site-packages/elastalert/elastalert.py", line 1897, in main client = ElastAlerter(args) File "/usr/lib/python2.7/site-packages/elastalert/elastalert.py", line 130, in init self.conf = load_rules(self.args) File "/usr/lib/python2.7/site-packages/elastalert/config.py", line 485, in load_rules raise EAException('Error loading file %s: %s' % (rule_file, e)) elastalert.util.EAException: Error loading file /etc/elastalert/conf.d/rules/rule.yaml: Error initiating alert ['email', 'compass_alerter.CompassAlerter']: Could not import module compass_alerter.CompassAlerter: No module named compass_alerter

I am not sure where to import the file that shows in error message could you please suggest me on this.

Prabhakarg1 avatar Jul 19 '18 20:07 Prabhakarg1

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.

Qmando avatar Jul 19 '18 23:07 Qmando

Thank you so much @Qmando python command works for me.

Prabhakarg1 avatar Jul 20 '18 12:07 Prabhakarg1