diff --git a/README.md b/README.md index 253d97f9a02275c0906de52a41e7122eb8434128..c48517d273965884552fe967b89b5448d3c1f0a5 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,4 @@ Pipe logs to Mattermost or Slack webhooks * Config file(s) * Init script with defaults override, restart, etc -* Replace PHP script with straight bash script * Make binary deps paths configurable? \ No newline at end of file diff --git a/pipe.sh b/pipe.sh index b32fce3c2110932b21c560eea87aa27dc3e5d1c0..7fbb3fcc787246a429c363297478a721d6e6bbb3 100755 --- a/pipe.sh +++ b/pipe.sh @@ -1,3 +1,3 @@ #!/bin/sh - -tail -Fq log.log | while read x ; do php im.php "$x" "#errors"; done +# TODO: path and config for send.sh +/usr/bin/tail -Fq log.log | while read x ; do send.sh "$x" "#errors" "danger"; done diff --git a/send.php b/send.php deleted file mode 100644 index 139611f7525e34ba3a0e473905ecccd2547725b7..0000000000000000000000000000000000000000 --- a/send.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -/** - * Parse log entry and format for webhook - * - * TODO: Switch to bash! This is a silly, vestigial implementation. - * - * @param string $message - * @param string $channel - * @param string $username - * @param string $icon_url - * @param string $webhook_url - * @return bool - */ -function im( $message = '', $channel = '#errors', $username = 'Error Bot', $icon_url = '', $webhook_url ) { - if ( ! is_string( $webhook_url ) ) { - return false; - } - - static $counter = 1; - - if ( '' == $message ) { - $message = 'debug ' . $counter; - $counter++; - } elseif ( is_array( $message ) || is_object( $message ) ) { - $message = var_export( $message, true ); - } - - // TODO: make an attachment? - $post_data = array( - 'channel' => $channel, - 'username' => $username, - 'icon_url' => $icon_url, - 'text' => "```\n{$message}\n```", - ); - - $curl = curl_init(); - - $body = json_encode( $post_data ); - - curl_setopt_array( - $curl, array( - CURLOPT_URL => $webhook_url, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_TIMEOUT => 10, - CURLOPT_CUSTOMREQUEST => 'POST', - CURLOPT_POSTFIELDS => $body, - CURLOPT_HTTPHEADER => array( - 'Content-Type: application/json', - 'Content-Length: ' . strlen( $body ), - ), - ) - ); - - curl_exec( $curl ); - curl_close( $curl ); - return true; -} - -$message = isset( $argv[1] ) ? $argv[1] : null; -$channel = isset( $argv[2] ) ? $argv[2] : null; -$username = isset( $argv[3] ) ? $argv[3] : null; -$icon_url = isset( $argv[4] ) ? $argv[4] : null; -$webhook_url = isset( $argv[5] ) ? $argv[5] : null; - -im( $message, $channel, $username, $icon_url, $webhook_url ); diff --git a/send.sh b/send.sh new file mode 100755 index 0000000000000000000000000000000000000000..b92de7edbd51170a406056fc4b6d8f8a1fd641cf --- /dev/null +++ b/send.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +MSG=${1:-Oops} +CHANNEL=${2:-#errors} +COLOR=${3:-default} + +# TODO: source these from a common config +USERNAME="Log bot" +CONTEXT="New log entry" +ICON_URL="" +WEBHOOK_URL="" + +/usr/bin/curl \ + -X POST \ + -s \ + --data-urlencode "payload={ \ + \"channel\": \"$CHANNEL\", \ + \"username\": \"$USERNAME\", \ + \"attachments\": [ { \ + \"fallback\": \"New log entry\", \ + \"pretext\": \"$CONTEXT\", \ + \"text\": \"$MSG\", \ + \"color\": \"$COLOR\" \ + } ],\ + \"icon_url\": \"$ICON_URL\" \ + }" \ + $WEBHOOK_URL