summaryrefslogtreecommitdiff
path: root/network/vnstat/rc.vnstat
blob: e17d7cdeebe7c9f0de4b9e1a84248239ba5ee067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
#
# /etc/rc.d/rc.vnstat - Start/stop/restart the vndstat  daemon.
# To make vnstat start automatically at boot, make this
# file executable: chmod 0755 /etc/rc.d/rc.vnstat
#

CONFIGFILE="/etc/vnstat.conf"
INTERFACE="eth0"

DBASEDIR="/var/lib/vnstat/"
PIDFILE="/var/run/vnstat.pid"

vnstat_start() {
  if [ -e "$PIDFILE" ]; then
    echo "vnstatd is already running under PID: $PIDFILE"
  elif [ -x /usr/sbin/vnstatd -a -r $CONFIGFILE ]; then
	if [ ! -e "$DBASEDIR/$INTERFACE" ]; then
	  # Sadly vnstat throws everything out over STDOUT instead of
	  # using STERR for warnings and such.
	  /usr/bin/vnstat -u -i "$INTERFACE" 1> /dev/null
      sleep 2
    fi
	echo "Starting the vnstat daemon for interface $INTERFACE..."
	/usr/sbin/vnstatd --config $CONFIGFILE --daemon
  fi
}

vnstat_stop() {
  # There be demons here. I am sure we can get the pid from /var/run/
  # for a much cleaner shutdown.
  echo -n "Stopping the vnstat Daemon..."
  if [ -r $PIDFILE ]; then
    kill $(cat $PIDFILE)
	sleep 2
	if [ -e "$PIDFILE" ]; then
	  rm $PIDFILE
	fi
	echo "Done"
  else
	killall vnstatd
	echo "Done"
  fi
}

vnstat_status() {
  pid="$(pidof $1)"
  if [ -n "$pid" ]; then
    echo "vnstat daemon is running with pid $pid"
  else
    echo "vnstat daemon is not running."
  fi
}

vnstat_restart() {
  echo "restarting the vnstart daemon..."
  vnstat_stop
  sleep 2
  vnstat_start
}

case "$1" in
  'start')
    vnstat_start
    ;;
  'stop')
    vnstat_stop
    ;;
  'restart'|'reload')
	vnstat_restart 
	;;
  'status')
	vnstat_status vnstatd
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
	exit 1
	;;
esac