<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[静怡家园]]></title> 
<link>http://www.zhanghaijun.com/index.php</link> 
<description><![CDATA[书山有路勤为径，学海无涯苦作舟！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[静怡家园]]></copyright>
<item>
<link>http://www.zhanghaijun.com/post//</link>
<title><![CDATA[工具控 InfluxDB+Grafana快速搭建自己的NewRelic,分析应用运行情况]]></title> 
<author>碟舞飞扬 &lt;webmaster@zhanghaijun.com&gt;</author>
<category><![CDATA[开源世界]]></category>
<pubDate>Thu, 12 Jan 2017 06:59:51 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//</guid> 
<description>
<![CDATA[ 
	NewRelic 估计很多人都用过，但是这货非常贵，贵的一般人买不起，尤其是个人项目，可咱也要性能指标分析啊！那来自己搭建一个<br/><a href="http://www.zhanghaijun.com/attachment.php?fid=222" target="_blank"><img src="http://www.zhanghaijun.com/attachment.php?fid=222" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>你需要三个工具:<br/><a href="http://influxdb.com/" target="_blank">InfluxDB</a> - Go 写的一个 Time series (不知道怎么翻译) 数据库，用于存储指标、事件、分析等数据；<br/><a href="http://grafana.org/" target="_blank">Grafana</a> - 一个纯静态的项目，用于访问 InfluxDB，自定义报表，也就是上面那个图的内容，可以自由编辑；<br/><a href="https://github.com/influxdb/influxdb-ruby" target="_blank">influxdb-ruby</a> - InfluxDB 的 Ruby 客户端库，用来写数据<br/>安装 InfluxDB<br/>InfluxDB 安装非常简单<br/>Mac<br/><div class="code">Homebrew 就可以安装<br/>$ brew update<br/>$ brew install influxdb</div><br/>Ubuntu<br/><div class="code"># for 64-bit systems<br/>wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb<br/>sudo dpkg -i influxdb_latest_amd64.deb<br/><br/># for 32-bit systems<br/>wget http://s3.amazonaws.com/influxdb/influxdb_latest_i386.deb<br/>sudo dpkg -i influxdb_latest_i386.deb</div><br/>然后启动就可以了，帐号默认 root 密码 root 你可以打开它的 Web Admin 界面: http://127.0.0.1:8083/ 并创建一个数据库 rails_app<br/>Grafana 安装<br/>由于 Grafana 是存静态的，你只需要下载源代码解压，将它部署在 Nginx 上面就可以了，或者可以用 Python 的 SimpleHTTPServer 来跑<br/><div class="code">$ wget http://grafanarel.s3.amazonaws.com/grafana-1.9.1.tar.gz<br/>$ tar zxf grafana-1.9.1.tar.gz<br/>$ cd grafana-1.9.1<br/>$ python -m SimpleHTTPServer<br/>Serving HTTP on 0.0.0.0 port 8000 ...<br/></div><br/>然后打开 http://127.0.0.1:8000 就可以看到 Grafana 的界面了，剩下的事情自己摸索。<br/>数据埋点<br/>gem install influxdb<br/>或 Gemfile 加入 influxdb<br/>Rails 项目 config/initializers/influxdb.rb<br/><textarea name="code" class="c" rows="15" cols="100">
require &#039;influxdb&#039;
influx_config&nbsp;&nbsp;= YAML.load_file(&quot;#&#123;Rails.root&#125;/config/influxdb.yml&quot;)[Rails.env]
$influxdb = InfluxDB::Client.new(&quot;rails_app&quot;, hosts: [&quot;127.0.0.1&quot;], port: 8086, username: &quot;root&quot;, password: &quot;root&quot;)

# 关注 ActionController 的 process_action 通知，会收到所有的请求
ActiveSupport::Notifications.subscribe(&#039;process_action.action_controller&#039;) do &#124;*args&#124;
&nbsp;&nbsp;event = ActiveSupport::Notifications::Event.new(*args)

&nbsp;&nbsp;info = &#123;
&nbsp;&nbsp;&nbsp;&nbsp;action: &quot;#&#123;event.payload[:controller]&#125;##&#123;event.payload[:action]&#125;&quot;, 
&nbsp;&nbsp;&nbsp;&nbsp;runtime: event.duration,
&nbsp;&nbsp;&nbsp;&nbsp;db_runtime: event.payload[:db_runtime],
&nbsp;&nbsp;&nbsp;&nbsp;server: Socket.gethostname,
&nbsp;&nbsp;&nbsp;&nbsp;status: event.payload[:status] 
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;$influxdb.write_point(&quot;process_action.action_controller&quot;, info)
end
</textarea><br/>然后数据就会慢慢的写入到 InfluxDB 的 rails_app 会有新表 process_action.action_controller 你可以回到 InfluxDb Admin 里面查询看看<br/>select * from process_action.action_controller;<br/>或者用 list series 查看所有的表<br/>list series;<br/>其它的选择<br/>除了用 Grafana 来展示统计的数据外，你还可以用 <a href="http://facette.io/" target="_blank">facette</a>，它也是支持 InfluxDB 的，也是一个 Go 写的 Web Server，包含 Web 界面，可以自由配置（目前看起来可用性没有 Grafana 好）。<br/>http://facette.io<br/><a href="http://www.zhanghaijun.com/attachment.php?fid=223" target="_blank"><img src="http://www.zhanghaijun.com/attachment.php?fid=223" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>2016 更新，用了 Grafana 3.0.0<br/>收集系统数据用 <a href="https://influxdata.com/time-series-platform/telegraf/" target="_blank">Telegraf</a><br/><a href="http://www.zhanghaijun.com/attachment.php?fid=224" target="_blank"><img src="http://www.zhanghaijun.com/attachment.php?fid=224" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>如何将influxdb的数据配置进grafana:<br/>https://www.rittmanmead.com/blog/2015/02/obiee-monitoring-and-diagnostics-with-influxdb-and-grafana/<br/>Tags - <a href="http://www.zhanghaijun.com/tags/influxdb/" rel="tag">influxdb</a> , <a href="http://www.zhanghaijun.com/tags/grafana/" rel="tag">grafana</a> , <a href="http://www.zhanghaijun.com/tags/newrelic/" rel="tag">newrelic</a>
]]>
</description>
</item><item>
<link>http://www.zhanghaijun.com/post//#blogcomment</link>
<title><![CDATA[[评论] 工具控 InfluxDB+Grafana快速搭建自己的NewRelic,分析应用运行情况]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>