Difference between revisions of "Instalasi MRTG"

From OnnoWiki
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
Networking → How to Install and Configure MRTG on Ubuntu Server
+
Sumber: https://www.howtoforge.com/tutorial/how-to-install-and-configure-mrtg-on-ubuntu-1804/
  
200 Comments
 
August 13th, 2009 by iceflatline
 
  
(20140528) – The steps in this post were amended to address changes in recent versions of software. Minor editorial corrections were also made. — iceflatline
 
  
This post will describe how to install and configure Tobi Oetiker’s MRTG (Multi Router Traffic Grapher) on your Ubuntu server. All steps assume that the Apache http server is installed and operating correctly. Once configured, you’ll be able to use MRTG to monitor the traffic in and out of your network using the SNMP capability in your network’s gateway\router. MRTG generates static HTML pages containing PNG images which provide a visual representation of this traffic. MRTG typically produces daily, weekly, monthly, and yearly graphs. MRTG is written in perl and works on Unix/Linux as well as Windows. MRTG is free software licensed under the GNU GPL.
+
MRTG juga dikenal sebagai "Multi Router Traffic Grapher" adalah tool yang free dan open source untuk memantau beban lalu lintas pada jaringan. Kita dapat memonitor lalu lintas jaringan harian, mingguan, bulanan, dan tahunan menggunakan MRTG. MRTG mendukung sistem operasi Windows dan Linux. MRTG menyediakan antarmuka web sederhana untuk menampilkan data lalu lintas jaringan dalam bentuk grafis.
  
Software versions used in this post were as follows:
+
==Kebutuhan==
Ubuntu Server v14.04 x64 LTS)
 
mrtg_2.17.4-2ubuntu2_amd64
 
  
So, let’s get started.
+
* Server Ubuntu 20.04
 +
* non-root user dengan sudo privilige
  
Download and Install
 
  
First, update your package list:
+
==Instalasi tool pendukung==
sudo apt-get update
 
  
Then download and install MRTG:
+
Instalasi paket
sudo apt-get install mrtg
 
  
If this is the first time installing MRTG on your server you’ll likely be presented with the following message (See Figure 1). Answering “Yes” means that the MRTG configuration file will not be readable by others (file permissions set to 640). Answering “No” means that the file is readable by others (file permissions set to 644). In this example we’re going to accept the default Yes.
+
sudo apt update
 +
sudo apt -y install apache2 snmp snmpd
  
Screenshot showing install message regarding MRTG file permissions
+
Edit
Figure 1
 
  
MRTG installs a sample configuration file /etc/mrtg.cfg used to hold the SNMP information obtained from your gateway\router. For a full listing of what MRTG installs and where, run the locate command:
+
vi /etc/snmp/snmpd.conf
sudo updatedb && locate mrtg
 
  
Technically speaking, mrtg.cfg could remain in /etc, but just to keep things tidy let’s create a directory for it and move it into that directory:
+
tambah
sudo mkdir /etc/mrtg && sudo mv /etc/mrtg.cfg /etc/mrtg
 
  
That’s it for installing MRTG. Now let’s move on and configure it.
+
rocommunity  public localhost
  
Configure
+
Restart apache & snmp
  
MRTG includes a script called cfgmaker that will help us populate /etc/mrtg/mrtg.cfg with the information obtained from your gateway\router. But before you run cfgmaker, you should setup the SNMP service in your gateway\router. This usually involves logging into your gateway\router and enabling SNMP. The default SNMP community name is typically “public.” If you change the SNMP community name to something else, make note of it. Now, run the following command, substituting your SNMP community name, if you’ve changed it, and adding the IP address of your gateway\router:
+
sudo systemctl start apache2
sudo cfgmaker --output=/etc/mrtg/mrtg.cfg public@your-router's-IP-address
+
sudo systemctl enable apache2
 +
sudo systemctl start snmpd
 +
sudo systemctl enable snmpd
  
If you would like to add more than one gateway\router to MRTG simply append the additional URL(s) to the same mrtg.cfg file. Then, when you build the web page using the indexmaker command described below, all the gateway\routers (and their associated graphs) will be displayed on the same HTML page.
+
==Instalasi & konfigurasi MRTG==
1 sudo cfgmaker --output /etc/mrtg/mrtg.cfg public@the-first-router's-IP-address public@the-second-router's-IP-address
 
  
Next, open /etc/mrtg/mrtg.cfg and make sure under Global Configuration Options that the lines “WorkDir: /var/www/mrtg” (under Debian), and “Options[_]: growright, bits” (under Global Defaults) are uncommented. Finally, add the following lines to the existing line EnableIPv6: nounder the Global Defaults section:
+
Instalasi MRTG
RunAsDaemon: Yes
 
Interval: 5
 
  
What does all this do? The line RunAsDaemon: Yes will enable MRTG to…um… run as a daemon. This is beneficial because MRTG is launched only once, thus the parsing of the /etc/mrtg/mrtg.cfg file is done only once, not repeatedly as would be the case if one were to run MRTG as a cron task – another acceptable method for running MRTG. Also, when running as a daemon, MRTG itself is responsible for timing the measurement intervals; therefore, we need to add the Interval line option and assign it a value – in this example 5. This means that every five minutes MRTG will poll the SNMP service in your gateway\router and update its graphs. We’ve also provided MRTG a directory to place its log file using the Logdir option, and finally, because many gateway\routers do not currently support SNMP over IPv6, we’ll retain the line EnableIPv6: no.
+
sudo apt -y install mrtg
  
Speaking of graphs, by default MRTG graphs grow to the left, so by adding the option “growright” the direction of the traffic visible in MRTG’s graphs flips causing the current time to be at the right edge of the graph and the history values to the left. We’ve also chosen the “bits” option, which means that the monitored traffic values obtained from your gateway\router are multiplied by 8 and displayed bits per second instead of bytes per second.
+
Buat directory & set permission di web
  
Okay, now it’s time to create the web pages which display the MRTG graphs using the indexmaker command. Run the following commands:
+
sudo mkdir -p /var/www/html/mrtg
sudo mkdir /var/www/mrtg
+
sudo chown -R www-data:www-data /var/www/html/mrtg
sudo indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg
 
  
Open /etc/apache2/apache.conf and add the following lines in the section containing similar Directory directives:
+
Buat konfigurasi MRTG
1 Alias /mrtg "/var/www/mrtg/"
 
2
 
3 <Directory "/var/www/mrtg/">
 
4         Options None
 
5         AllowOverride None
 
6         Require all granted
 
7 </Directory>
 
  
Then restart Apache:
+
sudo cfgmaker public@localhost > /etc/mrtg.cfg
sudo service apache2 restart
 
  
When restarting Apache you may receive a warning concerning its inability to “…reliably determine the server’s fully qualified domain name…” This non-critical error can be fixed by adding the following line to /etc/apache2/apache.conf:
+
Output kira2,
ServerName locahost:80
 
  
MRTG has been configured. Let’s start it up and see what it displays.
+
You should see the following output:
 +
--base: Get Device Info on public@localhost:
 +
--base: Vendor Id: Unknown Vendor - 1.3.6.1.4.1.8072.3.2.10
 +
--base: Populating confcache
 +
--base: Get Interface Info
 +
--base: Walking ifIndex
 +
--snpd:  public@localhost: -> 1 -> ifIndex = 1
 +
--snpd:  public@localhost: -> 2 -> ifIndex = 2
 +
--base: Walking ifType
 +
--snpd:  public@localhost: -> 1 -> ifType = 24
 +
--snpd:  public@localhost: -> 2 -> ifType = 6
 +
--base: Walking ifAdminStatus
 +
--snpd:  public@localhost: -> 1 -> ifAdminStatus = 1
 +
--snpd:  public@localhost: -> 2 -> ifAdminStatus = 1
 +
--base: Walking ifOperStatus
 +
--snpd:  public@localhost: -> 1 -> ifOperStatus = 1
 +
--snpd:  public@localhost: -> 2 -> ifOperStatus = 1
 +
--base: Walking ifMtu
 +
--snpd:  public@localhost: -> 1 -> ifMtu = 65536
 +
--snpd:  public@localhost: -> 2 -> ifMtu = 1500
 +
--base: Walking ifSpeed
 +
--snpd:  public@localhost: -> 1 -> ifSpeed = 10000000
 +
--snpd:  public@localhost: -> 2 -> ifSpeed = 1000000000
  
Start
+
Edit
  
There’s something important to keep in mind when starting MRTG, and that is that MRTG requires the environmental variable “LANG” to be C in order to run properly. Since most Linux systems these days, including Ubuntu server, use UTF-8 (run echo $LANG to see what your system uses), let’s change LANG to C and start MRTG using the following command:
+
vi /etc/mrtg.cfg
sudo env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log
 
  
When you run MRTG for the first time you may see a lot of complaints about missing log files. No worries, this is normal for the first 2-3 times you start MRTG this way. If, however, it continues to complain you may want to look into the source of the problem.
+
WorkDir: /var/www/html/mrtg
  
Well, that’s it. Now point your browser to http://your-server-address/mrtg and you should see a page that resembles Figure 2. You may have more or less graphs depending on the number of interfaces reported by your gateway\router(s).
+
Buat index.html
  
Screenshot showing the default MRTG web page
+
sudo indexmaker /etc/mrtg.cfg > /var/www/html/mrtg/index.html
Figure 2
 
  
Because of the aforementioned option in /etc/mrtg/mrtg.cfg, you’ll see the graph starting “grow” to the right as the traffic is monitored over time, and the Y axis displayed as bits per second. If you click on any one of these graphs you’ll be taken to another page showing individual graphs for 30 minute, two hour, and daily averages, along with the maximum, average, and current bit rate in and out of that particular interface. Only interested in displaying one particular interface? Don’t like the look of the page? No worries, just edit /etc/mrtg/mrtg.cfg and/or /var/www/mrtg/index.html until you get pages looking the way you want.
+
Konfigurasi Apache Web untuk MRTG
  
Okay, so now that we have MRTG installed, configured and running let’s move on and discuss how to keep it running.
+
sudo vi /etc/apache2/sites-available/mrtg.conf
  
Operate
+
Tambahkan,
  
Starting MRTG by hand is not ideal in the long run. So perhaps after you’ve done some tweaking on MRTG and are satisfied with the results, you can automate the process of running MRTG by using a startup script in your system startup sequence. Here’s the script that I use:
+
<VirtualHost *:80>
1 #! /bin/sh
+
ServerAdmin admin@yourdomain.com
2 ### BEGIN INIT INFO
+
  DocumentRoot "/var/www/html/mrtg"
3 # Provides:          mrtg
+
ServerName yourdomain.com
4 # Required-Start: 
+
<Directory "/var/www/html/mrtg/">
5 # Required-Stop:   
+
Options None
6 # Default-Start:    2 3 4 5
+
  AllowOverride None
7 # Default-Stop:      0 1 6
+
Order allow,deny
8 # Short-Description: mrtg init script
+
Allow from all
9 # Description:      This file is used to start, stop, restart,
+
  Require all granted
10 #                    and determined status of the mrtg daemon.
+
</Directory>
11 # Author:            iceflatline <iceflatline@gmail.com>
+
TransferLog /var/log/apache2/mrtg_access.log
12 ### END INIT INFO
+
  ErrorLog /var/log/apache2/mrtg_error.log
13  
+
  </VirtualHost>
14 ### START OF SCRIPT
 
15 set -e
 
16 # PATH should only include /usr/* if it runs after the mountnfs.sh script
 
17 PATH=/sbin:/usr/sbin:/bin:/usr/bin
 
18 DESC="mrtg"
 
19 NAME=mrtg
 
20 DAEMON=/usr/bin/$NAME
 
21 DAEMON_ARGS="/etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log"
 
22 PIDFILE=/etc/mrtg/$NAME.pid
 
23 SCRIPTNAME=/etc/init.d/$NAME
 
24  
 
25 # Exit if the mrtg package is not installed
 
26 [ -x "$DAEMON" ] || exit 0
 
27  
 
28 # Load the VERBOSE setting and other rcS variables
 
29 . /lib/init/vars.sh
 
30  
 
31 # Define LSB log_* functions.
 
32 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
 
33 . /lib/lsb/init-functions
 
34  
 
35 # Function that starts the mrtg daemon
 
36 start()
 
37 {
 
38     env LANG=C start-stop-daemon --start --quiet \
 
39     --exec $DAEMON -- $DAEMON_ARGS
 
40 }
 
41
 
42 # Function that stops the mrtg daemon
 
43 stop()
 
44 {
 
45     start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
 
46     --pidfile $PIDFILE
 
47 }
 
48
 
49 case "$1" in
 
50   start)
 
51     log_daemon_msg "Starting $DESC"
 
52     start
 
53     case "$?" in
 
54         0) log_end_msg 0 ;;
 
55         1) log_end_msg 1 ;;
 
56     esac
 
57     ;;
 
58   stop)
 
59     log_daemon_msg "Stopping $DESC"
 
60     stop
 
61     case "$?" in
 
62         0) log_end_msg 0 ;;
 
63         1) log_end_msg 1 ;;
 
64     esac
 
65     ;;
 
66   restart|force-reload)
 
67     log_daemon_msg "Restarting $DESC"
 
68     stop
 
69     case "$?" in
 
70       0|1)
 
71         start
 
72         case "$?" in
 
73             0) log_end_msg 0 ;;
 
74             1) log_end_msg 1 ;;
 
75         esac
 
76         ;;
 
77     esac
 
78     ;;
 
79     status)
 
80     status_of_proc "$DAEMON" "$NAME"
 
81     ;;
 
82   *)
 
83     echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}"
 
84     ;;
 
85 esac
 
86 exit 0
 
87 ### END OF SCRIPT
 
  
To use the script, save it to your home directory as mrtg and make it executable. Then move or copy it to /etc/init.d:
+
Enable site dan restart,
cd ~
 
chmod +x mrtg
 
sudo mv mrtg /etc/init.d/
 
  
Now, link the mrtg script to all of Ubuntu server’s multi-user run levels (2-5):
+
sudo a2ensite mrtg
sudo update-rc.d mrtg defaults
+
sudo systemctl restart apache2
  
Now, let’s start MRTG using our script. If it’s currently running then substitute restart for start in the following command:
+
==Akses==
sudo service mrtg start
 
  
That’s it. Now if for some reason your server is rebooted, MRTG should fire up automatically. If you would like to remove the MRTG script from the server’s multi-user run levels, use the command sudo update-rc.d -f mrtg remove).
+
MRTG bisa dilihat di URL
 
 
Conclusion
 
 
 
This concludes the post on how to install and configure MRTG on Ubuntu server. As you can see, MRTG isn’t terribly complicated and proves to be a really nice open source package for monitoring and displaying traffic in and out your network from virtually anywhere you have a web browser. For a full list of all the configuration options and other information I encourage you to visit the MRTG web site.
 
  
 +
http://yourdomain.com
 +
http://ip-address/mrtg/
  
 
==Referensi==
 
==Referensi==
  
* http://oss.oetiker.ch/mrtg/doc/index.en.html
+
* https://www.howtoforge.com/tutorial/how-to-install-and-configure-mrtg-on-ubuntu-1804/
* http://lqman.wordpress.com/2011/10/22/network-monitoring-dengan-mrtg-dan-shorewall-ip-accounting/
 
* https://www.iceflatline.com/2009/08/how-to-install-and-configure-mrtg-on-ubuntu-server/
 
  
 
==Pranala Menarik==
 
==Pranala Menarik==
  
* [[Linux Howto]]
+
* [[SNMP]]

Latest revision as of 09:21, 14 May 2020

Sumber: https://www.howtoforge.com/tutorial/how-to-install-and-configure-mrtg-on-ubuntu-1804/


MRTG juga dikenal sebagai "Multi Router Traffic Grapher" adalah tool yang free dan open source untuk memantau beban lalu lintas pada jaringan. Kita dapat memonitor lalu lintas jaringan harian, mingguan, bulanan, dan tahunan menggunakan MRTG. MRTG mendukung sistem operasi Windows dan Linux. MRTG menyediakan antarmuka web sederhana untuk menampilkan data lalu lintas jaringan dalam bentuk grafis.

Kebutuhan

  • Server Ubuntu 20.04
  • non-root user dengan sudo privilige


Instalasi tool pendukung

Instalasi paket

sudo apt update
sudo apt -y install apache2 snmp snmpd

Edit

vi /etc/snmp/snmpd.conf

tambah

rocommunity  public localhost

Restart apache & snmp

sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl start snmpd
sudo systemctl enable snmpd

Instalasi & konfigurasi MRTG

Instalasi MRTG

sudo apt -y install mrtg

Buat directory & set permission di web

sudo mkdir -p /var/www/html/mrtg
sudo chown -R www-data:www-data /var/www/html/mrtg

Buat konfigurasi MRTG

sudo cfgmaker public@localhost > /etc/mrtg.cfg

Output kira2,

You should see the following output:
--base: Get Device Info on public@localhost:
--base: Vendor Id: Unknown Vendor - 1.3.6.1.4.1.8072.3.2.10
--base: Populating confcache
--base: Get Interface Info
--base: Walking ifIndex
--snpd:   public@localhost: -> 1 -> ifIndex = 1
--snpd:   public@localhost: -> 2 -> ifIndex = 2
--base: Walking ifType
--snpd:   public@localhost: -> 1 -> ifType = 24
--snpd:   public@localhost: -> 2 -> ifType = 6
--base: Walking ifAdminStatus
--snpd:   public@localhost: -> 1 -> ifAdminStatus = 1
--snpd:   public@localhost: -> 2 -> ifAdminStatus = 1
--base: Walking ifOperStatus
--snpd:   public@localhost: -> 1 -> ifOperStatus = 1
--snpd:   public@localhost: -> 2 -> ifOperStatus = 1
--base: Walking ifMtu
--snpd:   public@localhost: -> 1 -> ifMtu = 65536
--snpd:   public@localhost: -> 2 -> ifMtu = 1500
--base: Walking ifSpeed
--snpd:   public@localhost: -> 1 -> ifSpeed = 10000000
--snpd:   public@localhost: -> 2 -> ifSpeed = 1000000000

Edit

vi /etc/mrtg.cfg
WorkDir: /var/www/html/mrtg

Buat index.html

sudo indexmaker /etc/mrtg.cfg > /var/www/html/mrtg/index.html

Konfigurasi Apache Web untuk MRTG

sudo vi /etc/apache2/sites-available/mrtg.conf

Tambahkan,

<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
DocumentRoot "/var/www/html/mrtg"
ServerName yourdomain.com
<Directory "/var/www/html/mrtg/">
Options None
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
TransferLog /var/log/apache2/mrtg_access.log
ErrorLog /var/log/apache2/mrtg_error.log
</VirtualHost>

Enable site dan restart,

sudo a2ensite mrtg
sudo systemctl restart apache2

Akses

MRTG bisa dilihat di URL

http://yourdomain.com
http://ip-address/mrtg/

Referensi

Pranala Menarik