Demo entry 2695288

Annexe 4

   

Submitted by anonymous on Sep 17, 2015 at 00:47
Language: Bash. Code size: 1.7 kB.

#!/bin/bash
# This script enable checks when the services in the array are installed

pattern_with_brackets() {
  # Transform "service" in "[s]ervice" for the command pgrep -f
  echo "[$(echo $1 | colrm 2)]$(echo $1 | colrm 1 1)"
}

activate_if_config_present() {
  service=$1
  default_config_path=/etc/$service
  config_path=${2:-$default_config_path}
  if test -e "$config_path"; then
    activate_service $service
  fi
}

activate_if_pgrep_process() {
  service=$1
  default_pattern=$service
  pattern=${2:-$default_pattern}
  if pgrep "$pattern"; then
    activate_service $service
  fi
}

activate_if_pgrep_full_process() {
  service=$1
  default_pattern=$service
  pattern=${2:-$default_pattern}
  if pgrep -f "$(pattern_with_brackets $pattern)"; then
    activate_service $service
  fi
}

activate_service() {
  service=$1
  if test -f /etc/dd-agent/conf.d/$service.yaml.example; then
    mv /etc/dd-agent/conf.d/$service.yaml.example /etc/dd-agent/conf.d/$service.yaml
    mv /etc/dd-agent/checks.d/$service.py.example /etc/dd-agent/checks.d/$service.py
    restart=true
  fi
}

restart_agent_if_needed() {
  if $restart; then
    service datadog-agent restart
  fi
}

restart=false
activate_if_config_present "elasticsearch"
activate_if_config_present "redis"
activate_if_config_present "nginx"
activate_if_config_present "grafana" /data/grafana
activate_if_config_present "apache2"
activate_if_config_present "graphite"
activate_if_pgrep_full_process "celery"
activate_if_config_present "postgresql"
activate_if_pgrep_process "gunicorn"
activate_if_config_present "carbon"
activate_if_pgrep_process "ssh" /usr/sbin/sshd
restart_agent_if_needed

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).