在 Ubuntu 12.04 上安装 Graphite 监控工具

有几个朋友都跟我提过 Graphite,监控工具里面的新秀,一直都没时间关注,最近把 n 台物理服务器用 Puppet 代码化的任务完成后有了不少空闲时间,抽时间看了看 Graphite,先搜了一下中文想看看大家对这个工具有啥看法,居然没人提到这个工具,哪怕一篇博客一个帖子都没有??

常收到一些学生朋友来信,问到一些 Linux、计算机方面的学习建议,VPSee 能给的第一个建议是学好英语,优秀的技术人员必须能熟练的翻阅英文资料,大家也看到了,可用的中文资料真的不多,虽然英文会议我们可以不去、英文技术会议的视频也可以不看,但是平时的一些技术交流邮件、Github、Mailing list、经典技术书籍、文档、好的技术博客等都需要英文。Eric Steven Raymond 在他的那篇有名的 How To Become A Hacker 里也提到了:

If you don’t have functional English, learn it.

如果不能用英语交流,Linus Torvalds 写出来的 Linux 可能也就出不了芬兰,也不会有今天的成功。

废话打住,来看 Graphite.

Graphite 是一个(可运行在廉价硬件上的)企业级开源监控工具,用于采集服务器实时信息并进行统计,可采集 n 个服务器实时状态,如:用户请求消息,Memcached 命中率,RabbitMQ 消息服务器状态、操作系统负载、等等。Graphite 使用 Python 编写,采用 Django 框架,使用自己的简单文本协议通讯,服务平均每分钟有4800次更新操作,简单的文本协议和强大的绘图功能可以方便地扩展到任何需要监控的系统上。

和其他监控工具不同的是,Graphite 自己本身并不收集具体的数据,这些数据收集的具体工作通常由第三方工具或插件完成(如 Ganglia, collectd, statsd, Collectl 等,参考:使用 collectd 和 Graphite 监控服务器)。所以上面说 “Graphite 是一个系统监控工具” 的说法不完全正确,更准确的说法应该是 “Graphite 是一个数据绘图工具”,得到数据后绘图,它并不关心具体数据,你甚至可以把每天收到的邮件数当作参数传给 Graphite 制成图,然后一段时间后就生产一个漂亮的邮件繁忙展示图,并可以看出趋势。

简单的说,Graphite 做两件事:1、存储数据;2、按需绘图。

安装必要软件包:

$ sudo apt-get install apache2 libapache2-mod-wsgi python-django \
python-twisted python-cairo python-pip python-django-tagging

用 pip 安装 whisper (简单的存放和操作数据的库), carbon (监控数据的 Twisted 守护进程) 和 graphite-web (Django webapp):

$ sudo pip install whisper
$ sudo pip install carbon
$ sudo pip install graphite-web

初始化配置,直接用 example 文件里的默认配置就可以:

$ cd /opt/graphite/conf/

$ sudo cp carbon.conf.example carbon.conf
$ sudo cp storage-schemas.conf.example storage-schemas.conf
$ sudo cp graphite.wsgi.example graphite.wsgi

修改 apache 配置,增加一个 vhost 或者偷懒下载一个配置文件覆盖 default,覆盖后需要重新 reload 配置:

$ wget http://launchpad.net/graphite/0.9/0.9.9/+download/graphite-web-0.9.9.tar.gz
$ tar -zxvf graphite-web-0.9.9.tar.gz
$ cd graphite-web-0.9.9
$ sudo cp examples/example-graphite-vhost.conf /etc/apache2/sites-available/default

sockets 最好不要放在 /etc/httpd/ 下面(不同 Linux 发行版本对不同目录的权限问题很混淆人),ubuntu 版本可以放在 /var/run/apache2 下,所以修改 default 文件里的 WSGISocketPrefix 部分:

$ sudo vi /etc/apache2/sites-available/default
...
WSGISocketPrefix /var/run/apache2/wsgi
...

$ sudo /etc/init.d/apache2 reload

初始化 graphite 需要的数据库,修改 storage 的权限,用拷贝的方式创建 local_settings.py 文件:

$ cd /opt/graphite/webapp/graphite/

$ sudo python manage.py syncdb
$ sudo chown -R www-data:www-data /opt/graphite/storage/
$ sudo cp local_settings.py.example local_settings.py

$ sudo /etc/init.d/apache2 restart

启动 carbon:

$ cd /opt/graphite/

$ sudo ./bin/carbon-cache.py start

浏览器访问 IP 地址后就可以看到 graphite web 界面:
graphite

评论 (24 Comments)

  1. 与rrdrool相比, Graphite有什么特点呢?

  2. graphite 使用的数据库工具 whisper 非常类似 rrdtool,最大不同的地方是 rrdtool 假设数据都进数据库,这种假设有时候会导致不正常数据被插入数据库之前就被抛弃(覆盖)了,所以最后数据不是那么的准确。

  3. 这个东西的性能怎么样?

  4. 我用在监测高性能计算的一些节点上,性能很好~

  5. 呵呵。Ubuntu安装Graphite,挺不错的文章。我就是从你这起步,谢谢你。
    如果有需要查看CentOS 5或6上的安装方法可以参考我网站上Graphite分类,还包括Graphite一些Faq,协作工具等。

  6. 为什么我到sudo pip install graphite-web这步的时候,会出现问题:cannot fetch index base URL http://pypi.python.org/simple/

  7. 用这个Graphite监控,用谁来报警呢?

  8. 照猫画虎失败了:

    Service Temporarily Unavailable

    The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

  9. $ sudo cp example-graphite-vhost.conf /etc/apache2/sites-available/default ,这一步有错!应该先cd example目录下在执行上面步骤

  10. @wopub
    真仔细,已更正~

  11. 博主,我新装的ubuntu 12.04 x86 64系统,完全按照您的步骤安装,安装完毕后,访问出现:
    Service Temporarily Unavailable
    The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

  12. 哈哈,博主,你文档存在问题,我找出原因了,你文档里面少了一个几步,我们用vim编辑器打开站点配置文件vim /etc/apache2/sites-available/default;里面第17行:
    WSGISocketPrefix /etc/httpd/wsgi/
    这个目录 /etc/httpd包括子目录wsgi默认是没有的,需要自己去建立

  13. @wopub
    嗯嗯,sockets 最好不要放在 /etc/httpd/ 下面,最好放在 /var/run/apache2 下,比如:WSGISocketPrefix /var/run/apache2/wsgi

  14. 有没有windows下的graphite呢?

  15. @thedarkside
    真的没有~

  16. 好人!!!

  17. Exception occurred processing WSGI script ‘/opt/graphite/conf/graphite.wsgi’.
    Traceback (most recent call last):
    File “/opt/graphite/conf/graphite.wsgi”, line 16, in
    import graphite.metrics.search
    File “/opt/graphite/webapp/graphite/metrics/search.py”, line 6, in
    from graphite.storage import is_pattern, match_entries
    File “/opt/graphite/webapp/graphite/storage.py”, line 7, in
    from graphite.remote_storage import RemoteStore
    File “/opt/graphite/webapp/graphite/remote_storage.py”, line 8, in
    from graphite.util import unpickle
    File “/opt/graphite/webapp/graphite/util.py”, line 73, in
    defaultUser = User.objects.create_user(‘default’,’default@localhost.localdomain’,randomPassword)
    File “/usr/lib/python2.7/dist-packages/django/contrib/auth/models.py”, line 139, in create_user
    user.save(using=self._db)
    File “/usr/lib/python2.7/dist-packages/django/db/models/base.py”, line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    File “/usr/lib/python2.7/dist-packages/django/db/models/base.py”, line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
    File “/usr/lib/python2.7/dist-packages/django/db/models/manager.py”, line 198, in _insert
    return insert_query(self.model, values, **kwargs)
    File “/usr/lib/python2.7/dist-packages/django/db/models/query.py”, line 1475, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
    File “/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py”, line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
    File “/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py”, line 735, in execute_sql
    cursor.execute(sql, params)
    File “/usr/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py”, line 234, in execute
    return Database.Cursor.execute(self, query, params)
    DatabaseError: attempt to write a readonly database

    楼主,这个是什么情况啊?

  18. 不用装mysql吗?

  19. 请问博主有没有常识单台host上实现多个carbon-cache实例??

  20. 访问地址,报表 no data,不能正常显示,点击dashboard,报错:
    Traceback (most recent call last):
    File “/usr/lib/python2.7/dist-packages/django/core/handlers/base.py”, line 99, in get_response
    request.path_info)
    File “/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py”, line 271, in resolve
    sub_match = pattern.resolve(new_path)
    File “/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py”, line 271, in resolve
    sub_match = pattern.resolve(new_path)
    File “/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py”, line 159, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
    File “/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py”, line 168, in _get_callback
    raise ViewDoesNotExist(“Could not import %s. Error was: %s” % (mod_name, str(e)))
    ViewDoesNotExist: Could not import graphite.dashboard.views. Error was: cannot import name timezone

  21. 小白弱弱问一句,全配好之后,浏览器里怎么访问?https://127.0.0.1/dashboard/#不能访问。应该怎么排错,请指点一下。

  22. 博主您好,按照您的文章,最后访问ip ,怎么出来的是 Ubuntu Logo Apache2 Ubuntu Default Page 界面,是Apache没配置好吗?应该怎么做?谢谢

  23. lz,为什么配置好以后ip访问,怎么出来的是 Ubuntu Logo Apache2 Ubuntu Default Page 界面,难道配置有问题?

  24. lz,为什么配置好以后ip访问,怎么出来的是 Ubuntu Logo Apache2 Ubuntu Default Page 界面,难道配置有问题?

    我也是!同求!

发表评论