mirror_zfs/cmd/zed/zed.d
Phil Kauffman aa9905d89b
zed-functions.sh: escape newline to produce valid json
This was discovered when using Discords Slack compatible webhook.

Slack webhooks works without the escape, however Discord rightly refuses
the POST as it contains invalid JSON.

https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook

Valid (while escaping the newline:
```
+ msg_json='{"text": "*ZFS scrub_finish error for test on quartz*\nZFS has detected a data error:\n\n   eid: 124\n class: scrub_finish\n  host: quartz\n  time: \n error: \n objid: :\n  pool: test\n"}'
```

Invalid (no escape):
```
+ msg_json='{"text": "*ZFS scrub_finish error for test on quartz*
ZFS has detected a data error:\n\n   eid: 124\n class: scrub_finish\n  host: quartz\n  time: \n error: \n objid: :\n  pool: test\n"}'
```
The new line gets rendered and not sent inside the JSON as intended.

```
++ curl -X POST https://discord.com/api/webhooks/{webhook.id}/{webhook.token}/slack --header 'Content-Type: application/json' --data-binary '{"text": "*ZFS scrub_finish error for test on quartz*
ZFS has detected a data error:\n\n   eid: 124\n class: scrub_finish\n  host: quartz\n  time: \n error: \n objid: :\n  pool: test\n"}'
+ msg_out='{"message": "Cannot send an empty message", "code": 50006}'
```

Test method:
`root@quartz:/etc/zfs/zed.d# export ZED_ZEDLET_DIR=/etc/zfs/zed.d; export ZEVENT_EID=124; export ZEVENT_SUBCLASS=scrub_finish; export ZEVENT_POOL=test; export ZED_NOTIFY_DATA=1; bash -x ./data-notify.sh`

Reviewed-by: Damian Szuberski <szuberskidamian@gmail.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Philip Kauffman <philip@kauffman.me>
Closes #13049
2022-02-03 14:31:57 -08:00
..
.gitignore systemd mount generator and tracking ZEDLET 2018-04-06 14:11:09 -07:00
all-debug.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
all-syslog.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
data-notify.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
generic-notify.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
history_event-zfs-list-cacher.sh.in Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
Makefile.am Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
pool_import-led.sh Enclosure LED fixes 2017-02-10 16:09:45 -08:00
README Fix broken URL 2017-02-15 11:15:38 -08:00
resilver_finish-notify.sh OpenZFS 5997 - FRU field not set during pool creation and never updated 2016-08-12 13:06:48 -07:00
resilver_finish-start-scrub.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
scrub_finish-notify.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
statechange-led.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
statechange-notify.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
trim_finish-notify.sh Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00
vdev_attach-led.sh Fix statechange-led.sh & unnecessary libdevmapper warning 2016-10-25 11:05:30 -07:00
vdev_clear-led.sh Multipath autoreplace, control enclosure LEDs, event rate limiting 2016-10-19 12:55:59 -07:00
zed-functions.sh zed-functions.sh: escape newline to produce valid json 2022-02-03 14:31:57 -08:00
zed.rc Add ShellCheck's --enable=all inside cmd/ 2022-01-06 16:07:54 -08:00

Shell scripts are the recommended choice for ZEDLETs that mostly call
other utilities and do relatively little data manipulation.

Shell scripts MUST work on both bash and dash.

Shell scripts MUST run cleanly through ShellCheck:
  http://www.shellcheck.net/

General functions reside in "zed-functions.sh".  Use them where applicable.

Additional references that may be of use:

  Google Shell Style Guide
  https://github.com/google/styleguide/blob/gh-pages/shell.xml

  Dash as /bin/sh
  https://wiki.ubuntu.com/DashAsBinSh

  Common shell script mistakes
  http://www.pixelbeat.org/programming/shell_script_mistakes.html

  Filenames and Pathnames in Shell: How to do it Correctly
  http://www.dwheeler.com/essays/filenames-in-shell.html

  Autoconf: Portable Shell Programming
  https://www.gnu.org/software/autoconf/manual/autoconf.html#Portable-Shell

Please BE CONSISTENT with the existing style, check for errors,
minimize dependencies where possible, try to be portable,
and comment anything non-obvious.  Festina lente.