2016年3月27日日曜日

[CentOS6] Graphite + collectd のインストール


CentOS6.7 に Graphite と Collectd をインストールしてリソース情報をグラフ化してみます。
Graphite と Collectd の詳細については、下記の公式ページを参照してください。


1.epel レポジトリのインストール


GraphiteとCollectd は eple レポジトリから yum でインストールします。
なので、まず、epel レポジトリを使えるようにします。
[root@node01 ~]# yum install -y epel-release

2.Graphite のインストール


GraphiteとApacheをインストールします。
[root@node01 ~]# yum -y install graphite-web python-carbon httpd

Apacheのデフォルト設定だと起動時にワーニングがでるので、設定変更します。
[root@node01 ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
[root@node01 ~]# vi /etc/httpd/conf/httpd.conf
[root@node01 ~]# diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
276c276
< ServerName 127.0.0.1:80
---
> #ServerName www.example.com:80

Apacheを自動起動するように設定して、サービスを開始します。
[root@node01 ~]# chkconfig httpd on
[root@node01 ~]# service httpd start
httpd を起動中:                                            [  OK  ]

Graphiteの設定を変更します。
[root@node01 ~]# cp /etc/graphite-web/local_settings.py /etc/graphite-web/local_settings.py.org
[root@node01 ~]# vi /etc/graphite-web/local_settings.py
[root@node01 ~]# diff /etc/graphite-web/local_settings.py /etc/graphite-web/local_settings.py.org
13c13
< SECRET_KEY = 'secret1234'
---
> #SECRET_KEY = 'UNSAFE_DEFAULT'
18c18
< ALLOWED_HOSTS = [ '*' ]
---
> #ALLOWED_HOSTS = [ '*' ]
23c23
< TIME_ZONE = 'Asia/Tokyo'
---
> #TIME_ZONE = 'America/Los_Angeles'

Graphiteのデータベース(SQLite)を作成します。
ここで、GraphiteのWEB画面のユーザを作成できるのですが、今回は、ここでは作らずに、後でユーザを作ります。
 [root@node01 ~]# python /usr/lib/python2.6/site-packages/graphite/manage.py syncdb
/usr/lib/python2.6/site-packages/django/conf/__init__.py:75: DeprecationWarning: The ADMIN_MEDIA_PREFIX setting has been removed; use STATIC_URL instead.
  "use STATIC_URL instead.", DeprecationWarning)
/usr/lib/python2.6/site-packages/django/core/cache/__init__.py:82: DeprecationWarning: settings.CACHE_* is deprecated; use settings.CACHES instead.
  DeprecationWarning
Creating tables ...
Creating table account_profile
Creating table account_variable
Creating table account_view
Creating table account_window
Creating table account_mygraph
Creating table dashboard_dashboard_owners
Creating table dashboard_dashboard
Creating table events_event
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_session
Creating table django_admin_log
Creating table django_content_type
Creating table tagging_tag
Creating table tagging_taggeditem

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

データベースファイルに Apacheがアクセスできるように、オーナを変更します。
[root@node01 lib]# chown apache.apache /var/lib/graphite-web/graphite.db

Carbon Cache の自動起動を設定してサービスを開始します。
[root@node01 ~]# chkconfig carbon-cache on
[root@node01 ~]# service carbon-cache start
Starting carbon-cache: Starting carbon-cache (instance a)
                                                           [  OK  ]

上記の設定を反映するために Apache を再起動します。
[root@node01 ~]# service httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中:                                            [  OK  ]

ブラウザで下記URLにアクセスしてWEB画面が表示できるか確認します。
  • http://<IPアドレス>/













3.Collectd のインストール


Collectdをインストールします。
[root@node01 ~]# yum install -y collectd

CollectdからGraphite にデータを送るプラグインは、Collectのバージョンによって異なり、次のとおり。
epel からインストールした Collectd のバージョンは 4.10 なので、今回は carbon_writer を使います。
carbon_writer は、別途インストールが必要です。
下記のようにディレクトリを作成して、Github からダウンロードして設置します。
[root@node01 ~]# mkdir /opt/collectd-plugins
[root@node01 ~]# cd /opt/collectd-plugins
[root@node01 collectd-plugins]# curl -OL https://raw.githubusercontent.com/indygreg/collectd-carbon/master/carbon_writer.py

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9118  100  9118    0     0  13154      0 --:--:-- --:--:-- --:--:-- 37834

Collectd の設定を変更します。
"Hostname"に設定した名前が、Graphite のWEB画面に表示されます。
"LineReceiverHost" は、Graphite をインストールしたサーバを指定します。
"MetricPrefix"に設定した名前でグルーピングできるようになります。
[root@node01 ~]# cp /etc/collectd.conf /etc/collectd.conf.org
[root@node01 ~]# vi /etc/collectd.conf
[root@node01 collectd-plugins]# diff /etc/collectd.conf /etc/collectd.conf.org
13c13
< Hostname    "192.168.56.11"
---
> #Hostname    "localhost"
23,43d22
< # sendto Graphite
< <LoadPlugin "python">
<     Globals true
< </LoadPlugin>
< <Plugin "python">
<     # carbon_writer.py is at path /opt/collectd-plugins/carbon_writer.py
<     ModulePath "/opt/collectd-plugins/"
<
<     Import "carbon_writer"
<
<     <Module "carbon_writer">
<         LineReceiverHost "192.168.56.11"
<         LineReceiverPort 2003
<         LineReceiverProtocol "tcp"
<         MetricPrefix "collectd"
<         DifferentiateCountersOverTime true
<         LowercaseMetricNames true
<         TypesDB "/usr/share/collectd/types.db"
<     </Module>
< </Plugin>
<

Collectd を自動起動するように設定して、サービスを起動します。
[root@node01 ~]# chkconfig collectd on
[root@node01 ~]# service collectd start
collectd を起動中: Initializing carbon_writer client in TCP socket mode.
                                                           [  OK  ]

Graphiteには、下図のように表示されます。
"collectd" 配下に表示されているのが、上記で Collectd をインストールしたサーバです。













見たいメトリックを選んで "value"をクリックすると、右のグラフに追加表示されます。また、グラフに表示中の "value" をクリックすると、グラフから削除されます。













複数のグラフを並べて見たい場合は、画面右上の "Dashbord" をクリックします。
下図のにように自分でグラフを作って見ることができます。














4.Graphite へのログイン


上記のGraphite用データベース構築時に作成しなかったGraphite 用のユーザを、ここで作成します。
[root@node01 ~]# python /usr/lib/python2.6/site-packages/graphite/manage.py createsuperuser
/usr/lib/python2.6/site-packages/django/conf/__init__.py:75: DeprecationWarning: The ADMIN_MEDIA_PREFIX setting has been removed; use STATIC_URL instead.
  "use STATIC_URL instead.", DeprecationWarning)
/usr/lib/python2.6/site-packages/django/core/cache/__init__.py:82: DeprecationWarning: settings.CACHE_* is deprecated; use settings.CACHES instead.
  DeprecationWarning
Username (leave blank to use 'root'): admin
E-mail address: admin@example.com
Password:
Password (again):
Superuser created successfully.

後で、パスワードを変更したい場合は、以下のコマンドを実行します。
python /usr/lib/python2.6/site-packages/graphite/manage.py changepassword

GraphiteのWEB画面を開き、ログインしてみます。
右上の [Login] クリックし、下図の画面が表示されたら上記で作成したユーザとパスワードでログインします。













下図のようにログインできれば、作成したグラフを保存できます。