Create a SysV Init script for Prometheus node exporter in old linux system (RHL6)

1.1k Views Asked by At

I have a server machine that has RHL6 (Red Hat Linux 6) and is based on SysV initialization (does not have systemd package), and I want to make my prometheus node exporter collect metrics from this machine.

All I can find online is how to create a node exporter service with systemctl (systemd): basically you create a .service file under /etc/systemd/system and then write something like this in it:

[Unit]
Description=Node Exporter

After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

And then you start the service, enable it at startup, etc with systemctl command like this

sudo systemctl start node_exporter
sudo systemctl status node_exporter
sudo systemctl enable node_exporter

But the problem is that I don't have systemd installed and I don't have the right to update the server machine system so I am trying to find a way how to write an init script for node exporter to be placed under /etc/rd.d/init.d in my case.

It seems that all scripts under init.d are shell scripts that declare many methods like start(), stop(), restart(), reload(), force_reload(), ...

So it's not as easy as writing the service based on systemd.

Anyone have an idea how to do that with SysV init ???

Thanks,

1

There are 1 best solutions below

0
On BEST ANSWER

I managed to found a solution for my problem.

Here is how the script looks like:

#!/bin/bash
#
# chkconfig: 2345 90 12
# description: node-exporter server
#

# Get function from functions library
. /etc/init.d/functions

# Start the service node-exporter
start() {
        echo -n "Starting node-exporter service: "
        /usr/sbin/node_exporter_service &
        ### Create the lock file ###
        touch /var/lock/subsys/node-exporter
        success $"node-exporter service startup"
        echo
}

# Restart the service node-exporter
stop() {
        echo -n "Shutting down node-exporter service: "
        killproc node_exporter_service
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/node-exporter
        echo
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status node_exporter_service
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0

We place the above script under /etc/init.d named "node-exporter" (without .sh) and we place the binary for the node exporter under /usr/sbin (with systemd we place binaries under /usr/local/bin).

You can download the binary file for node exporter from here https://github.com/prometheus/node_exporter/releases.

Then we add the script file to the list of services with command chkconfig --add node-exporter (to check if it already exists use command chkconfig --list node-exporter).

Enable the service with command chkconfig node-exporter on. And then to start/stop/restart ... we use command /etc/init.d/node-exporter start/stop/restart ....

In the start script we basically run the binary file and in the stop script we kill the process by its name.

I hope this will be useful.