github.com/influxdata/telegraf@v1.30.3/scripts/deb/post-install.sh (about)

     1  #!/bin/bash
     2  
     3  SCRIPT_DIR=/usr/lib/telegraf/scripts
     4  
     5  function install_init {
     6      cp -f $SCRIPT_DIR/init.sh /etc/init.d/telegraf
     7      chmod +x /etc/init.d/telegraf
     8  }
     9  
    10  function install_systemd {
    11      #shellcheck disable=SC2086
    12      cp -f $SCRIPT_DIR/telegraf.service $1
    13      systemctl enable telegraf || true
    14      systemctl daemon-reload || true
    15  }
    16  
    17  function install_update_rcd {
    18      update-rc.d telegraf defaults
    19  }
    20  
    21  function install_chkconfig {
    22      chkconfig --add telegraf
    23  }
    24  
    25  # Remove legacy symlink, if it exists
    26  if [[ -L /etc/init.d/telegraf ]]; then
    27      rm -f /etc/init.d/telegraf
    28  fi
    29  
    30  # Add defaults file, if it doesn't exist
    31  if [[ ! -f /etc/default/telegraf ]]; then
    32      touch /etc/default/telegraf
    33  fi
    34  
    35  # Add .d configuration directory
    36  if [[ ! -d /etc/telegraf/telegraf.d ]]; then
    37      mkdir -p /etc/telegraf/telegraf.d
    38  fi
    39  
    40  # If 'telegraf.conf' is not present use package's sample (fresh install)
    41  if [[ ! -f /etc/telegraf/telegraf.conf ]] && [[ -f /etc/telegraf/telegraf.conf.sample ]]; then
    42     cp /etc/telegraf/telegraf.conf.sample /etc/telegraf/telegraf.conf
    43  fi
    44  
    45  LOG_DIR=/var/log/telegraf
    46  test -d $LOG_DIR || mkdir -p $LOG_DIR
    47  chown -R -L telegraf:telegraf $LOG_DIR
    48  chmod 755 $LOG_DIR
    49  
    50  STATE_DIR=/var/lib/telegraf
    51  test -d "$STATE_DIR" || {
    52      mkdir -p "$STATE_DIR"
    53      chmod 770 "$STATE_DIR"
    54      chown root:telegraf "$STATE_DIR"
    55  }
    56  
    57  STATE_FILE="$STATE_DIR/statefile"
    58  test -f "$STATE_FILE" || {
    59      touch "$STATE_FILE"
    60      echo {} > "$STATE_FILE"
    61      chown root:telegraf "$STATE_FILE"
    62      chmod 660 "$STATE_FILE"
    63  }
    64  
    65  if [ -d /run/systemd/system ]; then
    66      install_systemd /lib/systemd/system/telegraf.service
    67      # if and only if the service was already running then restart
    68      deb-systemd-invoke try-restart telegraf.service >/dev/null || true
    69  else
    70  	# Assuming SysVinit
    71  	install_init
    72  	# Run update-rc.d or fallback to chkconfig if not available
    73  	if which update-rc.d &>/dev/null; then
    74  		install_update_rcd
    75  	else
    76  		install_chkconfig
    77  	fi
    78  	invoke-rc.d telegraf restart
    79  fi