浏览代码

Merge pull request #254 from hectcastro/hc-collectd

Merge pull request #254
Alex Payne 10 年前
父节点
当前提交
76d1a2b26e

+ 1
- 0
README.textile 查看文件

@@ -43,6 +43,7 @@ What do you get if you point this thing at a VPS? All kinds of good stuff!
43 43
 * Your own VPN server via "OpenVPN":http://openvpn.net/index.php/open-source.html.
44 44
 * An IRC bouncer via "ZNC":http://wiki.znc.in/ZNC.
45 45
 * "Monit":http://mmonit.com/monit/ to keep everything running smoothly (and alert you when it's not).
46
+* "collectd":http://collectd.org/ to collect system statistics.
46 47
 * Web hosting (ex: for your blog) via "Apache":https://www.apache.org/.
47 48
 * Firewall management via "Uncomplicated Firewall (ufw)":https://wiki.ubuntu.com/UncomplicatedFirewall.
48 49
 * Intrusion prevention via "fail2ban":http://www.fail2ban.org/ and rootkit detection via "rkhunter":http://rkhunter.sourceforge.net.

+ 206
- 0
roles/monitoring/files/etc_init.d_collectd 查看文件

@@ -0,0 +1,206 @@
1
+#! /bin/bash
2
+#
3
+# collectd - start and stop the statistics collection daemon
4
+# http://collectd.org/
5
+#
6
+# Copyright (C) 2005-2006 Florian Forster <octo@verplant.org>
7
+# Copyright (C) 2006-2009 Sebastian Harl <tokkee@debian.org>
8
+#
9
+
10
+### BEGIN INIT INFO
11
+# Provides:          collectd
12
+# Required-Start:    $local_fs $remote_fs
13
+# Required-Stop:     $local_fs $remote_fs
14
+# Should-Start:      $network $named $syslog $time cpufrequtils
15
+# Should-Stop:       $network $named $syslog
16
+# Default-Start:     2 3 4 5
17
+# Default-Stop:      0 1 6
18
+# Short-Description: manage the statistics collection daemon
19
+# Description:       collectd is the statistics collection daemon.
20
+#                    It is a small daemon which collects system information
21
+#                    periodically and provides mechanisms to monitor and store
22
+#                    the values in a variety of ways.
23
+### END INIT INFO
24
+
25
+. /lib/lsb/init-functions
26
+
27
+export PATH=/opt/collectd/sbin:/opt/collectd/bin:/sbin:/bin:/usr/sbin:/usr/bin
28
+
29
+DISABLE=0
30
+
31
+DESC="statistics collection and monitoring daemon"
32
+NAME=collectd
33
+DAEMON=/opt/collectd/sbin/collectd
34
+
35
+CONFIGFILE=/opt/collectd/etc/collectd.conf
36
+PIDFILE=/opt/collectd/var/run/collectd.pid
37
+
38
+USE_COLLECTDMON=1
39
+COLLECTDMON_DAEMON=/opt/collectd/sbin/collectdmon
40
+COLLECTDMON_PIDFILE=/opt/collectd/var/run/collectdmon.pid
41
+
42
+MAXWAIT=30
43
+
44
+# Gracefully exit if the package has been removed.
45
+test -x $DAEMON || exit 0
46
+
47
+if [ -r /etc/default/$NAME ]; then
48
+	. /etc/default/$NAME
49
+fi
50
+
51
+if test "$ENABLE_COREFILES" == 1; then
52
+	ulimit -c unlimited
53
+fi
54
+
55
+if test "$USE_COLLECTDMON" == 1; then
56
+	_PIDFILE="$COLLECTDMON_PIDFILE"
57
+else
58
+	_PIDFILE="$PIDFILE"
59
+fi
60
+
61
+# return:
62
+#   0 if config is fine
63
+#   1 if there is a syntax error
64
+#   2 if there is no configuration
65
+check_config() {
66
+	if test ! -e "$CONFIGFILE"; then
67
+		return 2
68
+	fi
69
+	if ! $DAEMON -t -C "$CONFIGFILE"; then
70
+		return 1
71
+	fi
72
+	return 0
73
+}
74
+
75
+# return:
76
+#   0 if the daemon has been started
77
+#   1 if the daemon was already running
78
+#   2 if the daemon could not be started
79
+#   3 if the daemon was not supposed to be started
80
+d_start() {
81
+	if test "$DISABLE" != 0; then
82
+		# we get here during restart
83
+		log_progress_msg "disabled by /etc/default/$NAME"
84
+		return 3
85
+	fi
86
+
87
+	if test ! -e "$CONFIGFILE"; then
88
+		# we get here during restart
89
+		log_progress_msg "disabled, no configuration ($CONFIGFILE) found"
90
+		return 3
91
+	fi
92
+
93
+	check_config
94
+	rc="$?"
95
+	if test "$rc" -ne 0; then
96
+		log_progress_msg "not starting, configuration error"
97
+		return 2
98
+	fi
99
+
100
+	if test "$USE_COLLECTDMON" == 1; then
101
+		start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
102
+			--exec $COLLECTDMON_DAEMON -- -P "$_PIDFILE" -- -C "$CONFIGFILE" \
103
+			|| return 2
104
+	else
105
+		start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
106
+			--exec $DAEMON -- -C "$CONFIGFILE" -P "$_PIDFILE" \
107
+			|| return 2
108
+	fi
109
+	return 0
110
+}
111
+
112
+still_running_warning="
113
+WARNING: $NAME might still be running.
114
+In large setups it might take some time to write all pending data to
115
+the disk. You can adjust the waiting time in /etc/default/collectd."
116
+
117
+# return:
118
+#   0 if the daemon has been stopped
119
+#   1 if the daemon was already stopped
120
+#   2 if daemon could not be stopped
121
+d_stop() {
122
+	PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
123
+
124
+	start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
125
+	rc="$?"
126
+
127
+	if test "$rc" -eq 2; then
128
+		return 2
129
+	fi
130
+
131
+	sleep 1
132
+	if test -n "$PID" && kill -0 $PID 2> /dev/null; then
133
+		i=0
134
+		while kill -0 $PID 2> /dev/null; do
135
+			i=$(( $i + 2 ))
136
+			echo -n " ."
137
+
138
+			if test $i -gt $MAXWAIT; then
139
+				log_progress_msg "$still_running_warning"
140
+				return 2
141
+			fi
142
+
143
+			sleep 2
144
+		done
145
+		return "$rc"
146
+	fi
147
+	return "$rc"
148
+}
149
+
150
+case "$1" in
151
+	start)
152
+		log_daemon_msg "Starting $DESC" "$NAME"
153
+		d_start
154
+		case "$?" in
155
+			0|1) log_end_msg 0 ;;
156
+			2) log_end_msg 1 ;;
157
+			3) log_end_msg 255; true ;;
158
+			*) log_end_msg 1 ;;
159
+		esac
160
+		;;
161
+	stop)
162
+		log_daemon_msg "Stopping $DESC" "$NAME"
163
+		d_stop
164
+		case "$?" in
165
+			0|1) log_end_msg 0 ;;
166
+			2) log_end_msg 1 ;;
167
+		esac
168
+		;;
169
+	status)
170
+		status_of_proc -p "$_PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
171
+		;;
172
+	restart|force-reload)
173
+		log_daemon_msg "Restarting $DESC" "$NAME"
174
+		check_config
175
+		rc="$?"
176
+		if test "$rc" -eq 1; then
177
+			log_progress_msg "not restarting, configuration error"
178
+			log_end_msg 1
179
+			exit 1
180
+		fi
181
+		d_stop
182
+		rc="$?"
183
+		case "$rc" in
184
+			0|1)
185
+				sleep 1
186
+				d_start
187
+				rc2="$?"
188
+				case "$rc2" in
189
+					0|1) log_end_msg 0 ;;
190
+					2) log_end_msg 1 ;;
191
+					3) log_end_msg 255; true ;;
192
+					*) log_end_msg 1 ;;
193
+				esac
194
+				;;
195
+			*)
196
+				log_end_msg 1
197
+				;;
198
+		esac
199
+		;;
200
+	*)
201
+		echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
202
+		exit 3
203
+		;;
204
+esac
205
+
206
+# vim: syntax=sh noexpandtab sw=4 ts=4 :

+ 4
- 1
roles/monitoring/handlers/main.yml 查看文件

@@ -1,2 +1,5 @@
1 1
 - name: restart monit
2
-  service: name=monit state=restarted
2
+  service: name=monit state=restarted
3
+
4
+- name: restart collectd
5
+  service: name=collectd state=restarted

+ 46
- 0
roles/monitoring/tasks/collectd.yml 查看文件

@@ -0,0 +1,46 @@
1
+- name: Install collectd dependencies
2
+  apt: pkg={{ item }} state=installed
3
+  with_items:
4
+    - libcurl4-openssl-dev
5
+    - librrd2-dev
6
+    - python-dev
7
+
8
+- name: Download collectd
9
+  get_url: url=http://collectd.org/files/collectd-{{collectd_version}}.tar.gz
10
+           dest=/root/collectd-{{collectd_version}}.tar.gz
11
+
12
+- name: Extract collectd
13
+  command: tar xzf collectd-{{collectd_version}}.tar.gz
14
+           chdir=/root creates=/root/collectd-{{collectd_version}}
15
+
16
+- name: Build and install collectd
17
+  shell: ./configure ; make all ; make install
18
+         executable=/bin/bash
19
+         chdir=/root/collectd-{{collectd_version}}
20
+         creates=/opt/collectd/sbin/collectdmon
21
+
22
+- name: Copy collectd init file into place
23
+  copy: src=etc_init.d_collectd dest=/etc/init.d/collectd mode=0755
24
+
25
+- name: Download collectd-librato plugin
26
+  get_url: url=https://github.com/librato/collectd-librato/archive/v{{collectd_librato_version}}.tar.gz
27
+           dest=/root/collectd-librato-{{collectd_librato_version}}.tar.gz
28
+  when: collectd_librato_email|length > 0
29
+
30
+- name: Extract collectd-librato plugin
31
+  command: tar xzf collectd-librato-{{collectd_librato_version}}.tar.gz
32
+           chdir=/root creates=/root/collectd-librato-{{collectd_librato_version}}
33
+  when: collectd_librato_email|length > 0
34
+
35
+- name: Install collectd-librato plugin
36
+  command: make install
37
+           chdir=/root/collectd-librato-{{collectd_librato_version}}
38
+           creates=/opt/collectd-librato-{{collectd_librato_version}}
39
+  when: collectd_librato_email|length > 0
40
+
41
+- name: Copy collectd configuration file into place
42
+  template: src=opt_etc_collectd.conf.j2 dest=/opt/collectd/etc/collectd.conf
43
+  notify: restart collectd
44
+
45
+- name: Ensure collectd is a system service
46
+  service: name=collectd state=started enabled=true

+ 1
- 0
roles/monitoring/tasks/main.yml 查看文件

@@ -1,2 +1,3 @@
1 1
 - include: monit.yml tags=monit
2 2
 - include: logwatch.yml tags=logwatch
3
+- include: collectd.yml tags=collectd

+ 32
- 0
roles/monitoring/templates/opt_etc_collectd.conf.j2 查看文件

@@ -0,0 +1,32 @@
1
+BaseDir     "/opt/collectd"
2
+
3
+LoadPlugin syslog
4
+LoadPlugin cpu
5
+LoadPlugin interface
6
+LoadPlugin load
7
+LoadPlugin memory
8
+LoadPlugin disk
9
+LoadPlugin df
10
+
11
+{% if (collectd_librato_email|length and collectd_librato_api_token|length) %}
12
+<LoadPlugin python>
13
+  Globals true
14
+</LoadPlugin>
15
+
16
+<Plugin python>
17
+  ModulePath "/opt/collectd-librato-{{ collectd_librato_version }}/lib"
18
+  Import "collectd-librato"
19
+
20
+  <Module "collectd-librato">
21
+    Email    "{{ collectd_librato_email }}"
22
+    APIToken "{{ collectd_librato_api_token }}"
23
+    TypesDB  "/opt/collectd/share/collectd/types.db"
24
+  </Module>
25
+</Plugin>
26
+{% else %}
27
+LoadPlugin rrdtool
28
+
29
+<Plugin rrdtool>
30
+  DataDir "/opt/collectd/var/lib/collectd/rrd"
31
+</Plugin>
32
+{% endif %}

+ 6
- 0
vars/defaults.yml 查看文件

@@ -27,6 +27,12 @@ ntp_servers:
27 27
   # - 2.north-america.pool.ntp.org
28 28
   # - 3.north-america.pool.ntp.org
29 29
 
30
+# collectd
31
+collectd_version: 5.4.1
32
+collectd_librato_version: 0.0.10
33
+collectd_librato_email: "" # (optional)
34
+collectd_librato_api_token: "" # (optional)
35
+
30 36
 # google authenticator
31 37
 google_auth_version: 1.0
32 38
 

正在加载...
取消
保存