Cacti – Creating a custom graph from a shell command
The following is a step-by-step guide on how to set up a Data Input Method, Data Template, Graph Template, Data Source, and Graph for Cacti. If you don’t know what Cacti is, check it out here.
Recently I’ve been having trouble with the number of threads that apache is spawning via prefork. I wanted to keep a constant monitor on just how many threads apache needed to spawn in order to handle requests. I also wanted to be able to see what times of day saw the most traffic and what the most and least number of active threads was at any one time.
Since I already had Cacti installed and running nicely on my server, the solution to this problem was obvious. I set out to create a custom data source and graph for cacti which would poll for the number of active apache processes every 5 minutes. To get this data, I use the following shell command:
ps -aux | grep -c www
This command returns the number of processes which are running under the www (apache) user. The following are instructions on how I used this simple console command to produce an informative graph in cacti:
First, navigate to the “Data Input Methods” page and click the “Add” link to add a new data input method. You can enter any name you’d like for the input method. For our purposes, you’ll need to set the “Input Type” to “Script/Command”. This setting allows you to execute any console command or run any script. The “Input String” field is the path to our script, along with input parameters. If you are using the results of a console command, simply type that command here. If you are executing a script, you will need to specify the full path to the script. Since we’re only executing a simple shell command, all you have to do is type in the following:
ps -aux | grep -c www
After clicking the “Save” button, you’ll be brought to the Input Method’s settings page. Here you can define names for your return values as well as define input values (these will be covered in a later post). For this simple example, we have no input fields and only one output field. Go ahead and click the “Add” button in the “Output Fields” section. This will bring up a new page with options for the output field name and friendly name. The output field name is the one returned by your console command or script. This value MUST EXACTLY match your command’s output. However, this is OPTIONAL for commands which only return a single result, such as the shell command we are running. If your script or shell command only returns a single value, you can type anything you want here. Be wary of the character limit for this field and note that no special characters are allowed. The friendly name can be anything you’d like it to be. Click the “create” button now.
Click the “Save” button to return to the Data Input Methods listing page.
Now that we have a data input method set up, we can configure a Data Template. As we did with the input method, navigate to the “Data Templates” page and click the “Add” link. Here you can enter anything you’d like for the Data Template name and Data Source name. Select the Data Input Method created in the previous step from the drop-down box.
The “Data Source Item” section is where you will enter all of your input method’s output fields. For the first one, go ahead and enter the name of our only return value, “numProc”. This value DOES NOT have to be the same as it was in the input method. This can be a friendly name. Leave the minimum value as-is and change the Maximum Value to 1000 (or whatever maximum value is appropriate for the range of values you expect this field to return). Leave the “Data Source Type” as “GAUGE”. This simply means that the values are collected as discrete points. See the cacti documentation for more information on other types. Now click the “Create” button.
Once the page reloads, you’ll see that our Data Source Item has a new option for “Output Field” and it has already been set to the only return value from our input method. You can now click the “Save” button at the bottom.
Next we create a Graph Template. As we did for “Data Templates”, navigate to the “Graph Templates” page and select “Add”. Enter any appropriate name you desire for both the Template Name and Graph Title. All other values on this page can be left default except for the “Upper Limit”. You’ll want to change that to be the maximum value you expect to plot on your graph. In our case, we’re going to set the value here to 1000. Now click the “create” button. The page will reload and you’ll be presented with yet more options for the graph template. The first thing we’ll do here is add an item by clicking “Add” in the “Graph Template Items” section.
In the Graph Template Item creation page, you’ll first want to select the data source for your graph item. If you’ve done everything correctly so far, you should see your Data Template and input field in the drop-down list. Once your data source is selected, you can set the other display values to anything you’d like. I set the color to red and selected “LINE1″ for the Graph Item Type. Finally, the “Text Format” format field value is the label that will be displayed in the graph legend. Type “Number of Processes” (or whatever describes your graph item) into this field. The cacti documentation provides more detail on what all of these options do.
Click the “create” button.
In addition to graphing a nice line, I also like to see what the last graphed value was in numeric form. To do this, click the “Add” button again to add another graph item. Select the same Data Source as before, but this time select GPRINT as the item type. Select “LAST” for the consolidation function and type in “Last” for “Text Format”. If you would also like average, max, and min values, just repeat the previous steps; changing “Consolidation Function” to AVERAGE, MIN, or MAX as appropriate.
Once you’ve finished with your graph settings, click the “save” button at the bottom.
Now that we have all of our templates set up, we need to create a data source for our server. Naviage to the “Data Sources” page and select “Add”. There aren’t many options on this page. Just select the data template we created earlier and then select the host machine and click “create” and then “save”.
Finally, all the pieces are in place and we can create an actual graph for our server. Navigate to “Graph Management” and click “Add”. Once again, select the graph template and host and click “create” and “save”.
Now the next time your cacti process updates its data sources, you should have a nice graph of apache processes!

Apache - Number of Processes