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 · 6 comments
Open

Execute command when log event matches a pattern #1311

sbhal opened this issue Sep 25, 2024 · 6 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.

@sbhal
Copy link
Author

sbhal commented Jan 28, 2025

I get following error while trying to exec "shell_exec"

2025-01-28T15:31:30.453+00:00 E t0 sql_util.cc:552 (1) unsafe use of shell_exec() in "
INSERT INTO lnav_events (content) VALUES (?)
"
2025-01-28T15:31:30.453+00:00 E t0 lnav.events.cc:164 unable to prepare event statement: unable to prepare SQL statement: unsafe use of shell_exec()
2025-01-28T15:31:30.453+00:00 E t0 sql_util.cc:552 (1) unsafe use of shell_exec() in "
INSERT INTO lnav_events (content) VALUES (?)
"
2025-01-28T15:31:30.453+00:00 E t0 lnav.events.cc:164 unable to prepare event statement: unable to prepare SQL statement: unsafe use of shell_exec()

@tstack
Copy link
Owner

tstack commented Jan 30, 2025

I get following error while trying to exec "shell_exec"

2025-01-28T15:31:30.453+00:00 E t0 sql_util.cc:552 (1) unsafe use of shell_exec() in "
INSERT INTO lnav_events (content) VALUES (?)
"

Ah, sorry, I forgot I had marked shell_exec() as only usable from top-level SQL and not within a TRIGGER. That's probably the right thing to do. That's probably the right thing to do though. So, we'll need to figure out another approach.

One option would be to ATTACH a SQLite DB on disk inside of lnav. You can then write your trigger to do an INSERT into a table in that DB. A separate process could poll the on-disk DB for changes and execute the commands as they come in.

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