Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute command when log event matches a pattern #1311

Open
sbhal opened this issue Sep 25, 2024 · 4 comments
Open

Execute command when log event matches a pattern #1311

sbhal opened this issue Sep 25, 2024 · 4 comments
Labels
enhancement Feature Request

Comments

@sbhal
Copy link

sbhal commented Sep 25, 2024

I want to use the lnav Events interface to search for regex in live log files and run a command when a match is found. Do you have any suggestions on how to do this?

Eventually, I want to search log files for when a long-running process finishes, and I want to be notified when it's done.

I also couldn't find a way to execute non-SQL commands when a log event occurs.

@sbhal sbhal added the enhancement Feature Request label Sep 25, 2024
@FaffeF
Copy link
Contributor

FaffeF commented Sep 25, 2024

Have you looked into shell_exec?
https://docs.lnav.org/en/latest/sqlext.html#shell-exec-cmd-input-options

@tstack
Copy link
Owner

tstack commented Sep 26, 2024

I also couldn't find a way to execute non-SQL commands when a log event occurs.

Are you trying to execute an lnav command or a shell program? Most of lnav's functionality can be accessed through SQL vtables.

@sbhal
Copy link
Author

sbhal commented Sep 29, 2024

I am trying to execute a shell command once log body matches a regex pattern.

I added a watch expression:

:config /log/watch-expressions/testfinished/expr regexp(:log_body, '.*Test run finished .*')

But I am noticing, there are no entries in lnav_events table using

;select * from lnav_events

I also have init sql

CREATE TRIGGER IF NOT EXISTS add_integ_tests_log_events
  AFTER INSERT ON lnav_events WHEN
    -- Check the event type
    jget(NEW.content, '/$schema') =
      'https://lnav.org/event-log-msg-detected-v1.schema.json' AND
    -- Only create the filter when a given format is seen
    jget(NEW.content, '/watch-name') = 'testfinished'
BEGIN
SELECT shell_exec('notify "test finished!"');
END;

@tstack
Copy link
Owner

tstack commented Oct 3, 2024

I am trying to execute a shell command once log body matches a regex pattern.

I added a watch expression:

:config /log/watch-expressions/testfinished/expr regexp(:log_body, '.*Test run finished .*')

I think you have the arguments to regexp() flipped around. The regular expression should come first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature Request
Projects
None yet
Development

No branches or pull requests

3 participants