Adding Custom Object Actions

We're nearly ready with the application, but there are two more functions that we need to implement. In the Virtual Host model we have a Boolean flag that indicates whether the host is the default. This information is also conveniently displayed in the listing. However, if we want to change it, we have to navigate to the object's edit page and change the setting there. It would be nice if this could be done from the object-list screen, by just selecting the appropriate object and using an...

Adding New Records

This is the functionality that requires user input. Since our model is fairly simple, there are only a few fields to fill in, specifically the IP address, network size, and description. A parent tree node will be automatically assigned depending on where the user is when clicking the Add link. For example, if the user navigates to and clicks an Add New IP link, the new record will automatically get 192.168.0.1 24 as a parent. There are two ways of handling data input in Django the hard way and...

Amazon Machine Images and Instances

The Amazon Machine Image (AMI) is the image of the operating system that can be started. The image contains all the packages that are required to run your system. You can have as many AMIs as you need. For example, if you wanted to replicate the two-tier web system that we described earlier, you would create two types of AMIs a web server AMI and a database AMI. The web server AMI would have the Apache web server and the Apache Tomcat application server packages installed. The database AMI...

Checking the MySQL Version

The very first check you may need to perform is the MySQL version number. It's quite important to keep your server installation up to date. Every new release fixes server software bugs and potentially introduces performance improvements. The plug-in that checks the current MySQL version bases its decision on the latest generally available (GA) release version number, which is available on the MySQL download page at http mysql. com downloads mysql . To extract this information from the web page,...

Checking Whether the Address Is In

Let's implement a simple function that checks whether the IP address is in use. To do so, we need to send an ICMP ECHO message to the IP address and wait for the response. Strictly speaking, this is not a valid test to check if address is in use, because there might be few scenarios when the IP address is used but does not respond to a ping request. Firewalls might be preventing ICMP traffic, or that traffic might be blocked at the server level. In most cases, however, this simple test is very...

Converting WSDL Schema to Python Helper Module

So far you have learned about the SOAP protocol (an XML-based protocol that defines how messages are encapsulated), RPC's way of communication (the client sends a message that tells what function it wants the server to execute, and the server responds with a message that contains the data generated by the remote function) and WSDL (the language that defines what methods are available and what data types are used in requests responses). We also decided that we are going to generate two helper...

Defining the Database Model

Before proceeding, please look back at Table 3-1 to review the fields we are going to use in the data model. Because Django maps objects to a relational database and does so automatically, we need to create a class definition for every concept that we are using in the application, which will be mapped to the tables in the database. We only have one table, so let's define the class for it as shown in Listing 3-2. Add this code to your models. py file just below the default contents. Listing 3-2....

Defining the Database Schema

From the requirements just stated, we know that we need to record the following data The IP range and or individual IP addresses The parent range that the current range belongs to For each record, whether it is allowed to be empty Before proceeding, I need to explain how IP addressing works, so you will better understand some specific database layout and structure decisions we're going to make. The description provided here is somewhat simplified if you want to learn more about IP networks and...

Extending the Design and Requirements

I mentioned support for DHCP as a requirement in the previous chapter, but what do we really want from it Let's take a look at how DHCP is used in a typical organization. I will assume the ISC DHCP service, which is widely available with most Linux distributions. When assigning addresses on a subnet, we have the following options Assign the IP addresses statically, in which case we configure each device with its own IP address. Assign the IP addresses dynamically, depending on a set of rules...

Format of Apache Log Files

The format of a log file is defined by the LogFormat directive in the Apache configuration file, which is typically either etc apache2 apache2.conf or etc httpd conf httpd.conf, depending on your Linux distribution. Here is an example LogFormat h l u t r > s b Referer i User-Agent i combined This configuration line is split into three parts. First is the directive name. The second part is the format string that defines the structure of the log line. We'll come back to the format string...

Generating the Configuration File

We've finished adjusting the administration interface, so it is now ready for adding new Virtual Hosts and managing the existing database entries. Now we need to finish writing the view method that will display the information. There is one issue though the parent directives mimic the XML syntax. That is, they have opening and closing elements. The default string representation that we've written for the VHostDirective model class takes care of the opening element, but we also need to write a...

Insert Into sensor Values Insert Into sensor Values Insert Into sensor VALUES

'cpu_load') 'memory') 'processes' ) -- Description Adds parameter list to sensor command -- and defines default thresholds id INTEGER PRIMARY KEY, sensor_id INTEGER, name TEXT, parameter TEXT, warning FLOAT, error FLOAT, FOREIGN KEY (sensor_id) REFERENCES sensor(id) Idle CPU ', Used CPU ', User CPU ', System CPU ' IO Wait CPU Free memory, Free memory, Used memory, Used memory, idle', NULL, NULL) used', NULL, NULL) user', NULL, NULL) 'system', NULL, NULL) , 'iowait', NULL, NULL) ', 'free_pct',...

Installing the Django Framework

I recommend that you download and use the latest Django code release from www.djangoproject .com. As of this writing, the latest version is 1.2, and all examples and code you'll find here are based upon this version of Django. The framework already went through a major upgrade from version 0.9x to 1.x, and I do not expect too many further changes, but if you are going to use a version other than 1.2, please read the release notes for any changes that might affect the functionality. Usually...

Installing the Required Libraries

The GeoIP database libraries are written in C, but there are Python bindings available as well. The packages are available on most Linux platforms. For example, on a Fedora system, run the following command to install these libraries sudo yum install GeoIP GeoIP-python This will install the C libraries along with the helper tools and the Python bindings. The package may include the initial database that contains the IP-to-country mapping data, but most likely, this data will be out of date, as...

Logpattern log Bzlogpattern logbz

The filename pattern is stored in the global variable OPTIONS.file_pattern. By default this is set to an empty string and so it will match all file names. This variable is controlled by the command-line parsing class, which I'm going to talk about later in the chapter. For the time being, just note that it can be set to any value by using the -p or --pattern option. I need to create a list of directories and all subdirectories recursively so that I can search for the log files in them. Users...

Plotting Performance Graphs

We've been referencing the images, but we haven't created any graphs yet. In this section, we'll look at the function that reads the data from our database and generates individual images for every possible host probe timescale combination. As you've seen, these images can be combined by multiple criteria. In this example, we group them by their timescale value and probe name. In addition to the simple data plotting, our function will also calculate some statistical parameters for the dataset...

Querying the Configuration Variables

You don't really need to connect to any of the databases if you want to retrieve the server configuration or the system status variables. It's enough to establish a connection to the database server. To get the MySQL variables, we will need to use the MySQL SHOW statement. Its syntax is similar to the SELECT statement, where you are allowed to use the LIKE and WHERE modifiers to limit the query set. (Remember that there are 287 configuration settings and 291 status variables ) We'll start with...

Querying the Server Status Variables

The server status variables provide an insight into the server operation by presenting the internal counters. All variables are read-only and cannot be modified. Note You can find detailed information about each of the 291 MySQL server status variables in the MySQL documentation, which is available at html. The SHOW command syntax is SHOW STATUS. Similar to the SHOW VARIABLES command without the modifier, this command returns the status applicable to the current session and is equivalent to the...

The Configuration Class Wrapper

We now know enough about the ConfigParser library to start using it, but before proceeding I'd like to show you how to hide all library methods and represent them as class methods. If you look at the configuration file, it is simply a set of parameters. So why not hide the complexity of the get and set methods and represent all of the data contained in the configuration file as class variables There are a few reasons for doing this. First, it simplifies access to the variables for example...

The Index Page

The index page is the simplest page on our web site. It requires a minimum amount of code to generate, because we don't need to do any calculations. We just pass in the list of hosts, which we've already generated in the class initialization method. The private class method loads the template and passes the host list to it t f open( s index.html self.location, 'w') f.write(t.render( 'hosts' self.hosts )) f.close() The template iterates through the list of hosts and generates links to the host...

User Agenti useragent

When we initialize the log reader object, we give it two optional arguments. The first argument sets the log format line as it is defined in the Apache configuration. The default will be assumed if no argument string is supplied. The other argument indicates the location of the log files. Once we have identified the log line format, we will create a list of the alternative directive names as defined in our mapping table. The keywords in the list will be in exactly the same order as the...

Using URL References in the Templates

Obviously the model code can only return one URL for each object. The model class as such has no visibility on the functionality of the application it is only designed to represent the data upon which the application operates. So usually a model instance returns the URL that is used to display the object, in other words a representation URL. In our application we have multiple functions associated with the data entities, such as add, delete, and modify. Since we have a well-defined URL...

Writing the Plugin Code

We need to decide which methods we're going to implement. We need to receive the information about each log line that is being processed. Therefore, the plug-in must implement the process() method, which will perform the country lookup and increase the appropriate counters. At the end of the loop, we need to print a simple report that lists all the countries and sorts the list by the number of requests. As you can see in Listing 6-3, we use only one field from the data structure and just ignore...

Parsing the HTML Pages with Beautiful Soup

Once the contents are loaded into the BeautifulSoup object, we can start dissecting the BBC front web page. To give you an idea of what we need to find on the page, Figure 8-1 illustrates a sample screenshot of the front page, where you can clearly see the position of the top story. The top story on the BBC News UK when I captured the screen was titled Cameron and Clegg We are united. The title obviously changes from article to article, but the layout of the web site rarely changes, and the top...

The Add Function

First of all, we need to know if we can provide Add DHCP network functionality for a subnet. The easiest and most logical way to do this is to check in the network display view whether there are any address entries that do not have the subnet size set to 32 bits. If so, then this subnet cannot have DHCP enabled otherwise, provide a link to the DHCP add function. So the network view is going to perform the check and pass a Boolean variable that we will query in the template and will either...

Calculating the Mean and Standard Deviation

Standard Deviation

Since we are going to build a reporting system that produces statistical reports about the behavior of our system, let's look at some of the statistical functions that we will be using. Quite possibly, the most commonly used function is for calculating the average value of a series of elements. The NumPy library provides two functions to calculate the average of all numbers in an array mean and average . The mean function calculates a simple mathematical mean of any given set of numbers. gt gt...

Generating the DHCP Configuration File

We have all the information we require, but it's not much use in its current form. All data is in the database tables, and although it spells out how the DHCP server should be configured, it cannot be used in this form. We need to write a view that will generate a configuration file, which the DHCP server will be able to understand. Let's go back and revisit what the DHCP configuration file should look like. Since we're using the ISC DHCP server, the configuration file including only those...

Nagios PlugIn Architecture

The power of Nagios NMS is in its plug-in architecture. All check commands are external utilities that can be written in any language C, Python, Ruby, Perl and so on. The plug-ins communicate with the Nagios system by means of OS return codes and the standard input output mechanism. In other words, Nagios has a predefined set of return codes that the check scripts must return. The return code dictates what the new service state should be set to. All return codes and the corresponding service...

Representing the Information in an ER Diagram

Diagram For Admin System

I now have defined all the tables that I will be using on the monitoring server. Each field is defined, along with the relations between the tables. Even though I have only a few tables, it is still sometimes confusing to find and visualize the relation between different tables. To make things easier, especially when writing SQL queries, it is a good idea to draw an entity relationship ER diagram, a concept introduced in Chapter 5. Figure 9-2 is the ER diagram I drew for the tables defined...

Finding the Trend Line of a Dataset

The sample application we'll build in this chapter should report on and help us visualize the trends of various readings. For example, let's say that we're collecting data about the CPU load. How can we find out if the load is gradually increasing over time An obvious way is to look at the graph of the readings, and the really pronounced trends will be visible immediately. But we don't want to need to look at all the possible graphs ourselves and try to spot a trend. If the increase in load is...

Using the subprocess Library

The subprocess module defines one class, which is used to spawn new processes the Popen class. The name of an external program is passed as the first argument to the Popen class constructor. You have two options when passing the command name use a string or use an array. These are treated differently depending on whether you use the shell to execute the command or not. The default setting is not to use the shell. In that case, the Popen class expects the first argument to be the name of the...

Enabling the Management Interface

There is very little you need to do to enable the administration interface add it to the applications list in the site configuration, enable URL rules, and finally configure Apache to serve static content for the interface mostly CSS and JS scripts . Modify the INSTALLED_APPS list in the settings. py module so that it contains the administration package 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin',...

Creating Web Pages with the Jinja Templating System

Django Template System

In the last section of this chapter we are going to create another script, this one generating a simple structure of web pages containing the graphs. The main entry page lists all available checks grouped by the system and links to the check details page. When a user navigates to that page, she will see the graph generated by RRDTool and some details about the check itself such as the check description and OID . Now, this looks relatively easy to implement, and most people would simply start...

Introduction to SNMP

Mib Oid Python

SNMP Simple Network Management Protocol is a UDP-based protocol used mostly for managing network-attached devices, such as routers, switches, computers, printers, video cameras, and so on. Some applications also allow access to internal counters via the SNMP protocol. SNMP not only allows you to read performance statistics from the devices, it can also send control messages to instruct a device to perform some action for example, you can restart a router remotely by using SNMP commands. There...

Fixing Issues with Citrix Netscaler WSDL

The locator class in our generated service access helper module NSStat_services. py is defined as shown in Listing 2-8. Listing 2-8. The Locator class definition NSStatPort_address http netscaler_ip soap def getNSStatPortAddress self return NSStatServiceLocator.NSStatPort_address def getNSStatPort self, url None, kw return NSStatBindingSOAP url or This is obviously wrong, because the service host name netscaler_ip is not a valid IP address it should have been 192.168.1.1 , nor is a valid domain...