code-test

顯示具有 Freebsd 標籤的文章。 顯示所有文章
顯示具有 Freebsd 標籤的文章。 顯示所有文章

2013年5月2日 星期四

【FreeBSD】網路流量統計 RRDTool + Cacti 統計

 

     

RRDtool 是原 MRTG 作者另外開發的套件,它可將系統吐出來的資料做 logging 並繪製成圖表

而系統所吐出來的資料是 based on SNMP ( Simple Network Management Protocol ),所以只要是支援 SNMP 的設備都可以用 RRDtool 來做記錄與分析

不過仔細看了一下 RRDtool 操作的語法算小複雜,因此本篇教學會再搭配 Cacti 來協助控制 RRDtool 繪製報表

我的安裝環境如下

[ 安裝所需套件 ]

首先我們先安裝 RRDtool

# cd /usr/ports/databases/rrdtool
# make install clean

pkg_add -r gobject-introspection

接下來安裝 Net-SNMP

# cd /usr/ports/net-mgmt/net-snmp
# make install clean

[ ] AX_SOCKONLY

[ ] DMALLOC

不選

3 安裝 Cacti

# cd /usr/ports/net-mgmt/cacti
# make install clean

=======================================================================
Cacti is now installed. If you intall it for the first time,
you may have to follow this steps to make it work correctly:

1. Create the MySQL database:
# mysqladmin --user=root create cacti
2. Create a mysql user/password for cacti:
   (change user and/or password if required)
# echo "GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'; FLUSH PRIVILEGES;" | mysql
3. Import the default cacti database:
# mysql cacti < /usr/local/share/cacti/cacti.sql
4. Edit /usr/local/share/cacti/include/config.php.
5. Add the line to cron jobs with the command:
# crontab -u cacti -e
*/5 * * * * /usr/local/bin/php /usr/local/share/cacti/poller.php > /dev/null 2>&1
6. Add alias in apache config for the cacti dir:
Alias /cacti "/usr/local/share/cacti/"
7. Be sure apache gives an access to the directory ('Allow from' keywords).
8. Open a cacti login page in your web browser and login with admin/admin.

If you update cacti, open a login page, an updating process
will start automatically.
=======================================================================

上述三個套件安裝完之後,就可以開始來設定囉!

[ 設定 SNMP ]

首先先來設定 SNMP 的部份

# cd /usr/local/share/snmp
# vi snmpd.conf

snmpd.conf 內容如下

rocommunity public  // 設定 SNMP Read-Only 的 Community
syslocation MyOffice-at-work  // 系統所在地
syscontact oscarxxx.xxx@gmail.com  // 管理者 E-mail

rocommunity public
syslocation MyOffice-at-work
syscontact oscarxxx.xxx@gmail.com

另外請在 /etc/rc.conf 加入一行 snmpd_enable="YES"

# echo snmpd_enable=”yes” >> /etc/rc.conf

加好了之後就來啟動 SNMP 吧

# /usr/local/etc/rc.d/snmpd start

[ 設定 Cacti ]

設定 Cacti 這部份看個人習慣,如果您已經有 phpMyAdmin 的話可以在裡面操作,或是透過指令方式來新建

原則上我們需要新建一個 Database、User 讓 Cacti 可以存放相關資料

指令操作如下,新增一個 Database 名稱為 cacti

# mysqladmin -u root -p create cacti
Enter password: 輸入 Root 的密碼

新增一個 User 名叫 cacti 讓它可以存取 cacti 這個 Database,密碼設定為 cactiuser

[root@fb91-lab /usr/ports/net-mgmt/cacti]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.28-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> GRANT ALL ON cacti.* TO cacti@localhost IDENTIFIED BY 'cactiuser';
Query OK, 0 rows affected (0.02 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

接下來我們要倒入 Database 相關的 Schema

# mysql -u cacti -p cacti < /usr/local/share/cacti/cacti.sql
Enter password: 以本教學為例輸入 justforcacti

Database 的 Schema 建好之後,接下來我們來去設定程式的部份

# cd /usr/local/share/cacti/include/
# vi /usr/local/share/cacti/include/config.php

config.php 內容如下,原則上要改的地方就只有 username 跟 password 而已

/* make sure these values refect your actual database/host/user/password */
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cactiuser";
$database_port = "3306";
/* Default session name - Session name must contain alpha characters */
#$cacti_session_name = "Cacti";

接下來新增排程設定,固定每 5 分鐘讓 Cacti 去取得資料

# crontab -u cacti -e

加入這一行即可

*/5 * * * * /usr/local/bin/php /usr/local/share/cacti/poller.php > /dev/null 2>&1

接下來我們要在 Apache 裡面設定 Cacti 的路徑

# vi /usr/local/etc/apache22/httpd.conf

在 httpd.conf 加入下面這幾行

Alias /cacti "/usr/local/share/cacti/"
<Directory "/usr/local/share/cacti/">
        Options None
        AllowOverride None
        Order allow,deny
        Allow from all
</Directory>

/usr/local/share/cacti/

Allow from 127.0.0.1 192.168.0.0/16 .example.comss

好了之後要重新啟動 Apache 讓它生效

# /usr/local/etc/rc.d/apache22 restart

[ 初始化 Cacti ]

網址列請打入 http://您的 IP or Domain/cacti

FreeBSD 中文字型

# cd /usr/ports/chinese/font-std
# make install clean

Cacti 還有一些外掛 可以使用

FreeBSD 安裝 phpmyadmin

 

安裝完FAMP後 接下來使用網頁管理 mysql 的部分 所以通常我的都是使用 phpmyadmin來做管理

 

# cd /usr/ports/databases/phpmyadmin/

# vmake install clean

 

****************************************************************************

===> Adjusting file ownership in /usr/local/www/phpMyAdmin

phpMyAdmin-suphp-3.5.3 has been installed into:

    /usr/local/www/phpMyAdmin

Please edit config.inc.php to suit your needs.

To make phpMyAdmin available through your web site, I suggest
that you add something like the following to httpd.conf:

    Alias /phpmyadmin/ "/usr/local/www/phpMyAdmin/"

    <Directory "/usr/local/www/phpMyAdmin/">
        Options none
        AllowOverride Limit

        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1 192.168.0.0/16 .example.com
    </Directory>

****************************************************************************

修改apache 設定檔

# vi /usr/local/etc/apache22/httpd.conf

Alias /phpmyadmin/ "/usr/local/www/phpMyAdmin/"

<Directory "/usr/local/www/phpMyAdmin/">
Options none
AllowOverride Limit

Order Deny,Allow
Deny from all
Allow from 127.0.0.1 192.168.0.0/16 .example.com
</Directory>

重新啟動 apache22

/usr/local/etc/rc.d/apache22 restart

連接你的主機

http://your.ip.address/phpmyadmin

打開網頁 就可以看到管理程式頁面

【FreeBSD9.1】用 Ports 安裝 Apache + PHP + MySQL

===================================
1. 安裝 MySQL
===================================
# cd /usr/ports/databases/mysql55-server
# make install clean
安裝好之後
複製 MySQL 的系統設定檔到 /etc 下
# cp /usr/local/share/mysql/my-midden.cnf /etc/my.cnf
設定 MySQL 資料庫權限,並且啟動它
# chown -R mysql:mysql /var/db/mysql
# echo “mysql_enable=yes” >> /etc/rc.conf
# /usr/local/etc/rc.d/mysql-server start

設定 MySQL Root 的密碼
# mysqladmin -u root password 新密碼

最後我們可以測試登入一下 MySQL,有出現以下畫面就 OK
# mysql -u root -p
Enter password: (請輸入密碼)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.60-log FreeBSD port: mysql-server-5.1.60
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Mysql 安裝完成

===================================
2. 安裝 Apache
===================================
# cd /usr/ports/www/apache22
# make install clean
===================================
3. 安裝 PHP
===================================
# cd /usr/ports/lang/php5
# make config install clean
記得要勾選此選項喔,不然 Apache 會不認得 PHP
APACHE     Build Apache module
再安裝 PHP Extensions
# cd /usr/ports/lang/php5-extensions
# make config install clean
建議加勾選 GD、MYSQL、MySQLI、MBSTRING、ZIP 和 ZLIB
===================================
4. 修改設定檔
===================================
首先設定 PHP 的設定檔,以後就是設定 php.ini 這檔
# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
修改 Apache 的設定
# vi /usr/local/etc/apache22/httpd.conf
在最上面加入
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到
DirectoryIndex index.html
把它變成
DirectoryIndex index.php index.html index.htm
設定一開機啟動 Apache & MySQL
# vi /etc/rc.conf
加入這三行
apache22_enable="YES"
apache22_http_accept_enable="YES"
mysql_enable="YES"
在 /usr/local/www/apache22/data 裡新增一個檔案叫 index.php,內容如下
<?php phpinfo(); ?>
重開機,大功告成!

Freebsd91-安裝 mysql+apache+php+phpmyadmin