greshake / i3status-rust

Very resourcefriendly and feature-rich replacement for i3status, written in pure Rust
GNU General Public License v3.0
2.88k stars 475 forks source link

Allow custom blocks to (configurably) indicate state via exit code #331

Closed OddBloke closed 4 years ago

OddBloke commented 5 years ago

I have a systemd job that from time-to-time fails; I've written a custom block which calls systemctl to find out what the status of the unit is. systemctl will return 0 if the unit is running as expected, or 1-4 if it isn't (with each return code meaning a different failure state).

I would like to extend the custom plugin to have configuration along the lines of:

[[block]]
block = "custom"
command = "systemctl ..."
good_exitcode = 0
warn_exitcode = 3
critical_exitcode = 4

When my command exits with those specific exitcodes, I would then like the custom plugin to set the state appropriately.

(This proposal is related to #165, but I believe it is complimentary. Integrating at the exit code level works with a broad range of existing programs without JSON-producing wrappers, but isn't as rich an interface for programs that are looking to integrate specifically with i3status-rust.)

atheriel commented 5 years ago

The question I have about this is that I'm not sure your example is very compelling: it's pretty arguable that any non-zero exit code from systemctl status would be worthy of a warning or error status. Anyone wanting to use this feature would likely have to write custom scripts to output very carefully-chosen exit codes, since most existing programs only issue non-zero exit codes for errors.

Isn't a straightforward alternative to add an option like warn_on_nonzero_exit = true instead?

nicholasfagan commented 5 years ago

With this being a custom script, any no zero exit can be changed to something else, i.e. systemctl status || exit 1. I think something that would only change status on a non zero exit code is useful. But I don't think it fully captures what this issue it about: to programmatically change the state of the block.

OddBloke commented 5 years ago

I agree in retrospect that the systemctlexample isn't very compelling. A more interesting use case, perhaps, is a specifically written custom script that can use exit codes to signal block state. This would make the custom block interface a little richer for people who are happy to write slightly more involved commands, without affecting the majority of use cases.

In a scenario where people are expected to be writing adapters anyway, perhaps my original proposal could be changed to a single configuration boolean? Something like warn_on_exit_1 = true would mean that exit code 1 would be a warning (instead of an error as under usual operation)?

What do you think?

ammgws commented 4 years ago

@OddBloke The JSON option that was just merged in #515 may be of use to you. Could use it like this:

i3status-rs config

[[block]]
block = "custom"
command = "<run your script>"
json = true

script (sorry this is in fish script, but you get the idea)

systemctl status random-unit
switch $status
    case 0
        set state Good
    case 3
        set state Warn
    case 4
        set state Critical
    case '*'
        set state Warn
end
echo {\"state\":\"$state\", \"text\": \"Something went wrong!"}

Please re-open otherwise.