Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Cacti 0.8 Beginner's Guide

You're reading from  Cacti 0.8 Beginner's Guide

Product type Book
Published in Mar 2011
Publisher Packt
ISBN-13 9781849513920
Pages 348 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Thomas Urban Thomas Urban
Profile icon Thomas Urban

Table of Contents (23) Chapters

Cacti 0.8Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Installing Cacti 2. Using Graphs to Monitor Networks and Devices 3. Creating and Using Templates 4. User Management 5. Data Management 6. Cacti Maintenance 7. Network and Server Monitoring 8. Plugin Architecture 9. Plugins 10. Threshold Monitoring with Thold 11. Enterprise Reporting 12. Cacti Automation for NOC Mobile Access / Administration Online Resources Further Information Pop Quiz Answers
Index

Chapter 6. Cacti Maintenance

So far, we have looked into installing and running a Cacti instance, but what about its maintenance and upkeep? This chapter will show you how to create backups and restores of your Cacti installation, as well as provide you with information on how to keep your Cacti instance clean of dead hosts and unnecessary files.

In this chapter we are going to:

  • Provide a short overview of maintaining Cacti

  • Explain the directory structure of Cacti

  • Describe backup and restore procedures

  • Look into log file management with logrotate

  • Clean up unused RRD files

  • Provide a short overview of the Cacti CLI functionality

Let's begin!

An introduction to Cacti maintenance


Cacti maintenance defines the standard procedures needed for backing up the database and managing the RRD files. As you've learned, the RRD files contain the polled performance data, whereas the database only holds configuration data. Let's take a look at both from a data management point of view.

Database

As you've seen in previous chapters, the MySQL is the main configuration repository for any Cacti installation and all your defined hosts, templates, and graph definitions are stored there.

RRD files

The RRD files contain all your performance data, but without the database you will be unable to link these files to their original devices and data sources. Cacti offers two different ways of storing these files. One method stores all RRD files within a single directory. The other method creates subdirectories based on the host and local graph ID.

The Cacti directory structure


Let's look at the default directory structure of Cacti and some of the important subdirectories:

docs

The docs directory contains the full Cacti manual in different formats. The manual can be used as a quick reference for the various Cacti topics and also provides information on how to debug some of the more common issues you'll face.

The Cacti manual can be viewed within your browser by going to the following URL (replacing <server> with your Cacti Server):

http://<server>/<cacti dir>/docs/html/index.html.

include

The include directory contains files such as config.php and global.php. As you know, the config.php file contains the database and Cacti URL settings.

install

The install directory contains the installation files as well those needed in order to upgrade a Cacti installation. Once you have set up Cacti, you can safely delete this directory.

log

The log directory contains all of the Cacti log files. This is where the cacti.log file resides...

Backup and restore procedures


As with any other application or data, you should always have well planned out and properly documented backup and restore procedures in place in order to keep your data secure. Backing up the Cacti data assures you that you're not going to lose weeks or even years of valuable performance data. Although the following steps show you how to backup Cacti to the local system, you should save the backup files to a remote system. Off-site backups are also a good idea.

The MySQL database

MySQL comes with a basic backup application called mysqldump which dumps the content of one or more databases out to a plain text file. You will be using this program to create your database backup file, but we'll further enhance its functionality by wrapping it in a shell script to automate the filename and compressions tasks.

Time for action – backup your Cacti database


  1. Logon to your Cacti system.

  2. Execute the following command to create a backup directory:

    mkdir /backup
    
  3. Execute the following command and replace <password> with your MySQL database password (all in one line):

    mysqldump --user=root --password=<password> --add-drop-table --databases cacti > /backup/cacti_database_backup.sql
    
  4. Verify that the backup file was created by issuing the following command:

    ls -l /backup
    
  5. Look at the file with the more command:

    more /backup/cacti_database_backup.sql
    

What just happened?

You just created a backup of your Cacti database which includes everything that is stored in your Cacti database, from user IDs to graph templates, and all the hosts added to your instance.

Although you now have a backup of your database, you don't have a backup of the actual database user. You will need to remember to create a database user for Cacti when restoring it to a freshly installed system.

Enhancing the database backup...

Time for action – backup your Cacti files


  1. Logon to your Cacti system.

  2. Go to the root directory:

    cd /
    
  3. Execute the following command to create a backup of all the files defined earlier:

    tar -czvpf /backup/cacti_files_20101004.tgz ./etc/cron.d/cacti ./etc/php.ini ./etc/php.d ./etc/httpd/conf ./etc/httpd/conf.d ./etc/spine.conf ./usr/local/spine ./var/www/html/cacti ./var/www/html/cacti-0.8.7g
    
  4. Verify that the backup file exists by issuing the following command:

    ls -l /backup
    
  5. A listing similar to the following will be displayed:

What just happened?

You just created your first complete backup that contains all the files and directories you defined and is also already compressed. Together with the database backup, you are now able to restore your Cacti installation.

Enhancing the database backup script

Now that you know how to backup the database and the Cacti-related files, you can add the tar command to the backup script which you created earlier.

Time for action – enhancing the backup script


  1. Open the backupCacti.sh script and add the following line between the FILENAME and BACKUPDIR line:

    TGZFILENAME="cacti_files_$DATE.tgz";
    
  2. Add the following two lines at the end of the script:

    # Create the Cacti files backup
    tar -czpf $BACKUPDIR$TGZFILENAME ./etc/cron.d/cacti ./etc/php.ini ./etc/php.d ./etc/httpd/conf ./etc/httpd/conf.d ./etc/spine.conf ./usr/local/spine ./var/www/html/cacti ./var/www/html/cacti-0.8.7g
    
  3. Remove any earlier backups from the backup directory:

    rm /backup/cacti_*
    
  4. Start the backup script:

    /bin/bash /backup/backupCacti.sh
    
  5. Verify whether the database and the files backup have been created:

    ls –l /backup
    

What just happened?

You have enhanced the script to also create a backup of the Cacti files. Now you can issue this single command to create a complete backup. The extra dot you have added to the paths of the tar command will allow you to extract that tar file in a subdirectory without accidently overwriting an existing...

Time for action – creating a cronjob


  1. Logon to your Cacti system.

  2. Create a crontab file:

    vi /etc/cron.d/cactiBackup
    
  3. Enter the following lines to this file:

    # Cacti Backup Schedule
    0 2 * * * root /bin/bash /backup/backupCacti.sh > /dev/null 2>&1
    
  4. Save the file and exit the editor.

What just happened?

You created a backup job which will start at 02:00 in the morning every day of the week. You now have a daily backup of your Cacti database and files.

Restoring from a backup

Now that you have a daily backup of your Cacti instance, what steps do you need to take to actually restore your backup? Let's have a look at the different tasks needed.

Restoring the Cacti database

Restoring the Cacti database involves using the standard mysql command.

Time for action – restoring the Cacti database


  1. Logon to your Cacti server.

  2. Change to the backup directory:

    cd /backup
    
  3. Extract the latest backup file:

    gunzip cacti_database_20101004.sql.gz
    
  4. Restore the database. Make sure to change the username and Cacti database name to fit your installation. Please note that this command is going to overwrite your existing database:

    mysql -u cactiuser -p cacti < cacti_database_20101004.sql
    

What just happened?

You restored your Cacti database from a backup. Please note that any changes you have made since the backup has been created, such as adding new devices to the server, will be lost.

Restoring the Cacti files

Restoring files from a backup will involve two methods:

  • Restoring all of the Cacti files

  • Restoring a single file

Restoring all Cacti files

Restoring all files is only necessary in case of a full disaster recovery like a hard disk failure. Thankfully, your backup should contain all necessary files to restore a Cacti installation from the ground...

Time for action – restoring all Cacti files


  1. Logon to your Cacti server.

  2. For this example, let's assume you have it copied into the /backup directory.

  3. Change to the root directory:

    cd /
    
  4. Extract the contents of the backup:

    tar –xzvpf /backup/cacti_files_20101004.tgz
    
  5. You will see the contents of the archive being displayed on the screen during the extraction process, as shown in the following screenshot:

What just happened?

You extracted the files from your backup. The –p option tells tar to maintain the permissions as they were at the time of the backup. By using the –v (verbose) option, the name of each file currently being extracted is displayed on the screen, allowing you to monitor its progress. You will need to restart all relevant services afterwards, or reboot the system to make all the changes to the configuration files active.

Tip

Restore the Cacti files to a separate system

If you intend to restore the backup archive to another Linux distribution, or just want to restore a single file...

Time for action – restoring the Cacti config.php file


  1. Logon to your system.

  2. Change to the /tmp directory:

    cd /tmp
    
  3. Create a directory named cactirestore and change to that directory:

    mkdir cactirestore
    cd cactirestore
    
  4. Extract the backup file to the cactirestore directory:

    tar –xzvpf /backup/cacti_files_20101004.tgz
    
  5. Check the tmp directory for the existence of the etc, var, and usr directories:

    ls -l
    
  6. Change to the Cacti directory under var:

    cd var/www/html/cacti/
    
  7. Use the pwd command to check if you are in the correct directory:

    [root@localhost cacti]# pwd
    /tmp/cactirestore/var/www/html/cacti
    
  8. Copy the config.php file from the include directory to your existing Cacti installation:

    cp include/config.php /var/www/html/cacti/include
    
  9. Change to the root directory and delete the temporary restore location:

    cd /
    rm –rf /tmp/cactirestore
    

What just happened?

You just restored your config.php file from a backup. You can use this method to restore any file from your backup archive, such as corrupted...

Log file management


Cacti is able to create a log file with information on what it is doing, but depending on the log settings, this log file can become huge and can stop Cacti from working properly. Therefore you will need to introduce some log file management.

Fortunately, Linux comes with a tool for this called logrotate. This tool is able to manage any log file on the system and is already configured to do so for most of the files in /var/log. Let's look at how you can configure it to also manage your Cacti logs.

Time for action – configuring Logrotate


  1. Logon as root to your Cacti system.

  2. Create a new file in the /etc/logrotate.d directory using vi:

    vi /etc/logrotate.d/cactilog
    
  3. Insert the following code:

    /var/www/html/cacti/log/cacti.log {
        daily
        rotate 7
        copytruncate
        compress
        notifempty
        missingok
    }
  4. Save the file and quit.

  5. Check if the configuration file is correct:

    logrotate /etc/logrotate.conf –v
    
  6. You should see text similar to the following somewhere in the output:

What just happened?

You just created a configuration file for logrotate to manage the Cacti log. The settings you entered will make sure that logrotate will rotate the file on a daily basis, keeping 7 days worth of logs and compressing the old files at the same time. Logrotate will not rotate the file if it is empty and will also not throw an error if the file is missing.

Cacti maintenance


You have now learned how to perform some general maintenance tasks for making backups and managing the Cacti log file. Let's look into some more specific Cacti maintenance tasks.

List RRD files with no associated host

After running a Cacti installation for some time, data items and files may get orphaned as you delete and recreate graphs. Let's check for the common issue of left-over RRD files.

Time for action – finding orphaned RRD files


  1. Logon to your Cacti system.

  2. Change to the rra directory of your Cacti installation:

    cd /var/www/html/cacti/rra
    
  3. List the directory content:

    ls –l
    
  4. Keep a note of the RRDs listed as shown in the preceding screenshot.

  5. Login to your Cacti web interface as an admin user.

  6. Go to Management | Data Sources to list all the data sources:

  7. You can now compare the list of data sources with the RRD files listed on the command line. In the example shown, the RRD file named heute_de_ping_8.rrd does not exist in the web interface and is therefore an orphaned file.

What just happened?

You have used the CLI and the Cacti GUI to find unused RRD files which can be removed from the file system. As you create more graphs and data sources, the manual process quickly becomes overwhelming, so you are going to build a short script to perform this task for you.

Tip

The RRDCleaner plugin provides the same functionality but adds this to the Cacti GUI itself.

Automating the orphaned...

A short overview of the Cacti CLI functionality


As you've learned in previous chapters, Cacti comes with several CLI scripts suitable for some of Cacti's maintenance tasks. The CLI scripts can be found in the cli directory of the base Cacti installation.

The cli directory contains two repair scripts which you may want to use in case of database corruption or template errors, and if you do not want to lose all the configuration data since your last backup.

Repairing templates

It may happen that some of the data or graph templates have errors. You can then try to repair these templates by using the repair_templates.php script. Issue the following command to check for errors. Using the –execute parameter will help fix any errors shown:

cd /var/www/html/cacti/cli
php repair_templates.php

The following output will be shown on a working system:

Repairing the database

Cacti also provides a repair utility for checking and repairing the database structure. Using the repair_database.php script may save...

Summary


You should now be able to create automated backups of your Cacti installation as well as do some basic maintenance work.

Specifically, you've covered:

  • Creating an automated database and file backup—how to create a full Cacti database backup together with all-important Cacti data files

  • Restoring from a backup—how to restore a database backup and restoring single or all Cacti files

  • Managing the Cacti log file—how to include the Cacti log file in logrotate's configuration

  • Basic Cacti file maintenance—how to check for orphaned RRD files

  • Using Cacti repair utilities—how to use the Cacti CLI scripts for repairing templates and the database

You should now be able to perform some basic Cacti maintenance tasks. In the next chapter, you're going to learn how to retrieve performance data from network devices and servers using various methods. You're going to configure Windows servers and VMWare ESX systems so you can monitor the performance of these within your Cacti installation. You're also going...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Cacti 0.8 Beginner's Guide
Published in: Mar 2011 Publisher: Packt ISBN-13: 9781849513920
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}