Browse Source

Fix .chihuahuarc script and use strict mode for setup script

sh doesn't support "source", so we're now using "eval"

Strict mode is achieved with "set -eu": http://redsymbol.net/articles/unofficial-bash-strict-mode/
tags/v1.4
Moritz Marquardt 7 months ago
parent
commit
849142f917
2 changed files with 6 additions and 3 deletions
  1. +1
    -1
      check.go
  2. +5
    -2
      setup.go

+ 1
- 1
check.go View File

@@ -21,7 +21,7 @@ func RunCheck(checkName string, check *types.Check, shell []string) {
logg.Tag("check", checkName).Debug("Executing command: %#v", append(shell, check.Command))
ctx, cancel := context.WithTimeout(context.Background(), ConnectionTimeout)
defer cancel()
c := exec.CommandContext(ctx, shell[0], append(shell[1:], "[ -e ~/.chihuahuarc ] && source ~/.chihuahuarc; " + check.Command)...)
c := exec.CommandContext(ctx, shell[0], append(shell[1:], "[ -e ~/.chihuahuarc ] && eval \"$(cat ~/.chihuahuarc)\"; "+check.Command)...)
var errbuf bytes.Buffer
c.Stderr = &errbuf
output, err := c.Output()

+ 5
- 2
setup.go View File

@@ -3,12 +3,14 @@ package chihuahua
import "strings"

func SetupScript(keys string) string {
return `#!/bin/sh
return `#!/bin/sh
set -eu

# Install basic monitoring-plugins
apt-get install -y monitoring-plugins

# Create user "chihuahua" and add authorized SSH keys
useradd -M -d /var/chihuahua -r -s /bin/sh chihuahua
useradd -M -d /var/chihuahua -r -s /bin/sh chihuahua || true
mkdir -p /var/chihuahua/.ssh
echo 'export PATH="/var/chihuahua/:/usr/lib/nagios/plugins/:/usr/local/bin:/usr/bin:/bin"' > /var/chihuahua/.chihuahuarc

@@ -32,5 +34,6 @@ chmod +x /usr/local/bin/check_sudo /var/chihuahua/check_sudo
grep chihuahua /etc/sudoers >/dev/null || { echo 'chihuahua ALL=(root) NOPASSWD: /usr/local/bin/check_sudo' >> /etc/sudoers; }

chown -R chihuahua:chihuahua /var/chihuahua
echo Setup complete.
`
}

Loading…
Cancel
Save