使用 Grafana、collectd 和 InfluxDB 打造现代监控系统
想打造 New Relic 那样漂亮的实时监控系统我们只需要 InfluxDB/collectd/Grafana 这三个工具,这三个工具的关系是这样的:
采集数据(collectd)-> 存储数据(InfluxDB) -> 显示数据(Grafana)。
- InfluxDB 是 Go 语言开发的一个开源分布式时序数据库,非常适合存储指标、事件、分析等数据,看版本号(v0.8.8)就知道这个项目还很年轻;
- collectd 就不用介绍了吧,C 语言写的一个系统性能采集工具;
- Grafana 是纯 Javascript 开发的前端工具,用于访问 InfluxDB,自定义报表、显示图表等。
下面的安装和配置步骤在 Ubuntu 14.04 Server 64bit 版上完成。升级整个系统后重启:
<span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>
<span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> upgrade</span>
<span class="pln">$ sudo reboot</span>
安装 InfluxDB
InfluxDB 是 Go 写的,不依赖任何其他包或库,很干净。安装很容易:
<span class="pln">$ wget https</span><span class="pun">:</span><span class="com">//s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb</span>
<span class="pln">$ sudo dpkg </span><span class="pun">-</span><span class="pln">i influxdb_latest_amd64</span><span class="pun">.</span><span class="pln">deb</span>
启动 InfluxDB:
<span class="pln">$ sudo </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">influxdb start</span>
<span class="typ">Setting</span><span class="pln"> ulimit </span><span class="pun">-</span><span class="pln">n </span><span class="lit">65536</span>
<span class="typ">Starting</span><span class="pln"> the process influxdb </span><span class="pun">[</span><span class="pln"> OK </span><span class="pun">]</span>
<span class="pln">influxdb process was started </span><span class="pun">[</span><span class="pln"> OK </span><span class="pun">]</span>
启动后打开 web 管理界面 http://192.168.2.183:8083/ 默认用户名和密码是 root 和 root. InfluxDB 的 Web 管理界面端口是 8083,HTTP API 监听端口是 8086,如果需要更改这些默认设定,修改 InfluxDB 的配置文件 /opt/influxdb/current/config.toml 后重启 InfluxDB 就可以了。
在刚安装好的 InfluxDB 上创建一个名为 collectd 的数据库,可以用命令行创建,也可以用 Web 管理界面操作:
<span class="pln">$ curl </span><span class="str">"http://192.168.2.183:8086/db?u=root&p=root"</span><span class="pun">-</span><span class="pln">d </span><span class="str">"{\"name\": \"collectd\"}"</span>
安装 collectd
安装 collectd:
<span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install collectd</span>
配置 collectd 为客户端,收集到数据后直接发给 InfluxDB:
<span class="pln">$ sudo vi </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">collectd</span><span class="pun">/</span><span class="pln">collectd</span><span class="pun">.</span><span class="pln">conf</span>
<span class="pun">...</span>
<span class="typ">LoadPlugin</span><span class="pln"> network</span>
<span class="pun">...</span>
<span class="pun"><</span><span class="typ">Plugin</span><span class="pln"> network</span><span class="pun">></span>
<span class="typ">Server</span><span class="str">"192.168.2.183"</span><span class="str">"25826"</span>
<span class="pun"></</span><span class="typ">Plugin</span><span class="pun">></span>
<span class="pun">...</span>
重启 collectd:
<span class="pln">$ sudo </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">collectd restart</span>
InfluxDB 现在自带一个 collectd 插件来获取 collectd 客户端发来的数据,以前可没这么方便哦,0.8.4 版本以前只能通过 influxdb-collectd-proxy 这样的第三方程序来连接 collectd 和 InfluxDB. 如果你检查一下服务器上打开的端口就会发现 influxdb 插件启动了一个 25826 端口,如果发现 InfluxDB 数据库里没有(收集到)数据,务必检查这个 25826 端口是否正常启动了:
<span class="pln">$ sudo netstat </span><span class="pun">-</span><span class="pln">tupln</span>
<span class="typ">Active</span><span class="typ">Internet</span><span class="pln"> connections </span><span class="pun">(</span><span class="pln">only servers</span><span class="pun">)</span>
<span class="typ">Proto</span><span class="typ">Recv</span><span class="pun">-</span><span class="pln">Q </span><span class="typ">Send</span><span class="pun">-</span><span class="pln">Q </span><span class="typ">Local</span><span class="typ">Address</span><span class="typ">Foreign</span><span class="typ">Address</span><span class="typ">State</span><span class="pln"> PID</span><span class="pun">/</span><span class="typ">Program</span><span class="pln"> name</span>
<span class="pln">tcp </span><span class="lit">0</span><span class="lit">0</span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:</span><span class="lit">22</span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:*</span><span class="pln"> LISTEN </span><span class="lit">622</span><span class="pun">/</span><span class="pln">sshd</span>
<span class="pln">tcp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">8086</span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">668</span><span class="pun">/</span><span class="pln">influxdb</span>
<span class="pln">tcp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">22</span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">622</span><span class="pun">/</span><span class="pln">sshd</span>
<span class="pln">tcp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">8090</span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">668</span><span class="pun">/</span><span class="pln">influxdb</span>
<span class="pln">tcp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">8099</span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">668</span><span class="pun">/</span><span class="pln">influxdb</span>
<span class="pln">tcp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">8083</span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">668</span><span class="pun">/</span><span class="pln">influxdb</span>
<span class="pln">udp6 </span><span class="lit">0</span><span class="lit">0</span><span class="pun">:::</span><span class="lit">25826</span><span class="pun">:::*</span><span class="lit">668</span><span class="pun">/</span><span class="pln">influxdb</span>
InfluxDB 自带的 collectd 插件默认是关闭的,需要手动配置打开 enabled = true,并填上 database = “collectd” 这一行,这里的 “collectd” 就是我们上面创建的那个数据库,更改配置后记得重启 InfluxDB:
<span class="pln">$ sudo vi </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">influxdb</span><span class="pun">/</span><span class="pln">current</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">toml</span>
<span class="pln">$ sudo vi </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">influxdb</span><span class="pun">/</span><span class="pln">shared</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">toml</span>
<span class="pun">...</span>
<span class="com"># Configure the collectd api</span>
<span class="pun">[</span><span class="pln">input_plugins</span><span class="pun">.</span><span class="pln">collectd</span><span class="pun">]</span>
<span class="pln">enabled </span><span class="pun">=</span><span class="kwd">true</span>
<span class="com"># address = "0.0.0.0" # If not set, is actually set to bind-address.</span>
<span class="com"># port = 25826</span>
<span class="pln">database </span><span class="pun">=</span><span class="str">"collectd"</span>
<span class="com"># types.db can be found in a collectd installation or on github:</span>
<span class="com"># https://github.com/collectd/collectd/blob/master/src/types.db</span>
<span class="com"># typesdb = "/usr/share/collectd/types.db" # The path to the collectd types.db file</span>
<span class="pun">...</span>
<span class="pln">$ sudo </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">influxdb restart</span>
<span class="typ">Setting</span><span class="pln"> ulimit </span><span class="pun">-</span><span class="pln">n </span><span class="lit">65536</span>
<span class="typ">Setting</span><span class="pln"> ulimit </span><span class="pun">-</span><span class="pln">n </span><span class="lit">65536</span>
<span class="pln">influxdb process was stopped </span><span class="pun">[</span><span class="pln"> OK </span><span class="pun">]</span>
<span class="typ">Setting</span><span class="pln"> ulimit </span><span class="pun">-</span><span class="pln">n </span><span class="lit">65536</span>
<span class="typ">Starting</span><span class="pln"> the process influxdb </span><span class="pun">[</span><span class="pln"> OK </span><span class="pun">]</span>
<span class="pln">influxdb process was started </span><span class="pun">[</span><span class="pln"> OK </span><span class="pun">]</span>
现在 InfluxDB 已经准备好接受和处理 collectd 传来的数据了。用命令行或者 Web 管理界面验证一下数据库里是否有数据:
<span class="pln">$ curl </span><span class="pun">-</span><span class="pln">G </span><span class="str">'http://192.168.2.183:8086/db/collectd/series?u=root&p=root&q=list+series&pretty=true'</span>
<span class="pun">[</span>
<span class="pun">{</span>
<span class="str">"name"</span><span class="pun">:</span><span class="str">"list_series_result"</span><span class="pun">,</span>
<span class="str">"columns"</span><span class="pun">:</span><span class="pun">[</span>
<span class="str">"time"</span><span class="pun">,</span>
<span class="str">"name"</span>
<span class="pun">],</span>
<span class="str">"points"</span><span class="pun">:</span><span class="pun">[</span>
<span class="pun">[</span>
<span class="lit">0</span><span class="pun">,</span>
<span class="str">"192.168.2.183/cpu-0/cpu-idle"</span>
<span class="pun">],</span>
<span class="pun">...</span>
<span class="pun">]</span>
<span class="pun">}</span>
<span class="pun">]</span>
安装 Grafana
下载 grafana 后解压放到 web 服务器上就可用。这里省去配置 Nginx/Apache 之类的麻烦,直接用最简单的 Web 服务器 Python -m SimpleHTTPServer 驱动:
<span class="pln">$ wget http</span><span class="pun">:</span><span class="com">//grafanarel.s3.amazonaws.com/grafana-1.9.1.tar.gz</span>
<span class="pln">$ tar xzvf grafana</span><span class="pun">-</span><span class="lit">1.9</span><span class="pun">.</span><span class="lit">1.tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="pln">$ cd grafana</span><span class="pun">-</span><span class="lit">1.9</span><span class="pun">.</span><span class="lit">1.tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="pln">$ cp config</span><span class="pun">.</span><span class="pln">sample</span><span class="pun">.</span><span class="pln">js config</span><span class="pun">.</span><span class="pln">js</span>
<span class="pln">$ vi config</span><span class="pun">.</span><span class="pln">js</span>
<span class="pun">...</span>
<span class="com">// InfluxDB example setup (the InfluxDB databases specified need to exist)</span>
<span class="pln">datasources</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln">influxdb</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln">type</span><span class="pun">:</span><span class="str">'influxdb'</span><span class="pun">,</span>
<span class="pln">url</span><span class="pun">:</span><span class="str">"http://192.168.2.183:8086/db/collectd"</span><span class="pun">,</span>
<span class="pln">username</span><span class="pun">:</span><span class="str">'root'</span><span class="pun">,</span>
<span class="pln">password</span><span class="pun">:</span><span class="str">'root'</span><span class="pun">,</span>
<span class="pun">},</span>
<span class="pun">...</span>
<span class="pun">},</span>
<span class="pun">...</span>
<span class="pln">$ sudo python </span><span class="pun">-</span><span class="pln">m </span><span class="typ">SimpleHTTPServer</span>
用浏览器访问 Grafana,这里的默认端口是 8000: