This is the basic boiler plate needed to build even the most basic plugin.

Directory structure

plugins
  • myplugin
    • setup.php
    • myplugin.sql
    • index.php
    • myplugin.php
    • images
      • index.php
      • tab_myplugin.png

Files

setup.php
This file sets up all your funtions that hook into cacti, tabs, form elements, utilities, etc. as a bare minimum the top 3 functions are required. Possible Hooks
example:
<?php
/*******************************************************************************

    Author ......... Your name here
    Contact ........ yourname@youremail.com
    Home Site ...... http://yourwebsite.com
    Program ........ your plugin name
    Version ........ 0.1beta
    Purpose ........ example plugin

*******************************************************************************/


function plugin_init_myplugin() {
    // This is where you hook into the plugin archetecture
    global $plugin_hooks;
    $plugin_hooks['draw_navigation_text']['myplugin'] = 'myplugin_draw_navigation_text';
    $plugin_hooks['api_device_save']['myplugin'] = 'myplugin_api_device_save';
    $plugin_hooks['config_arrays']['myplugin'] = 'myplugin_config_arrays';
    $plugin_hooks['config_settings']['myplugin'] = 'myplugin_config_settings';
    $plugin_hooks['top_header_tabs']['myplugin'] = 'myplugin_show_tab';
    $plugin_hooks['top_graph_header_tabs']['myplugin'] = 'myplugin_show_tab';
}

function myplugin_version () {
    return array( 'name'    => 'myplugin',
            'version'   => '0.1',
            'longname'  => 'My Plugin Example for Cacti',
            'author'    => 'Your name here',
            'homepage'  => 'http://yourwebsite.com',
            'email' => 'yourname@youremail.com',
            'url'      => 'http://cactiusers.org/cacti/versions.php'
            );
}

function myplugin_draw_navigation_text ($nav) {
   $nav["myplugin.php:"] = array("title" => "Example", "mapping" => "index.php:", "url" => "myplugin.php", "level" => "1");
   return $nav;
}

function myplugin_api_device_save ($save) {
    if (isset($_POST['myplugin']))
        $save["myplugin"] = form_input_validate($_POST['myplugin'], "myplugin", "", true, 3);
    else
        $save['myplugin'] = form_input_validate('', "myplugin", "", true, 3);
    return $save;
}

function myplugin_config_arrays () {
    $realm_id=99;
    global $user_auth_realms, $user_auth_realm_filenames;
    $user_auth_realms[$realm_id]='Example';
    $user_auth_realm_filenames['myplugin.php'] = $realm_id;
}

function myplugin_config_settings () {
//this puts the following form elements on the Settings->Misc tab in cacti
    global $tabs, $settings;
    $tabs["misc"] = "Misc";

    $temp = array(
        "myplugin_header" => array(
            "friendly_name" => "My Plugin",
            "method" => "spacer",
            ),
        "myplugin_textbox" => array(
            "friendly_name" => "Arbitrary Textbox",
            "description" => "This is an arbitrary textbox",
            "method" => "textbox",
            "max_length" => 20,
            ),
        "myplugin_check" => array(
            "friendly_name" => "Arbitrary Checkbox",
            "description" => "This is an arbitrary checkbox",
            "method" => "checkbox",
            "default" => "on",
            ),
    );
    if (isset($settings["misc"]))
        $settings["misc"] = array_merge($settings["misc"], $temp);
    else
        $settings["misc"]=$temp;
}

function myplugin_show_tab () {
    //This checks to see if user has access to the tab and if so displays it
    global $config, $user_auth_realms, $user_auth_realm_filenames;
    $realm_id2 = 0;

    if (isset($user_auth_realm_filenames{basename('myplugin.php')})) {
        $realm_id2 = $user_auth_realm_filenames{basename('myplugin.php')};
    }
    if ((db_fetch_assoc("select user_auth_realm.realm_id
        from user_auth_realm where user_auth_realm.user_id='"
. $_SESSION["sess_user_id"] . "'
        and user_auth_realm.realm_id='$realm_id2'"
)) || (empty($realm_id2))) {
        print '<a href="' . $config['url_path'] . 'plugins/myplugin/myplugin.php"><img src="' . $config['url_path'] . 'plugins/myplugin/images/tab_myplugin.gif" alt="My Plugin" align="absmiddle" border="0"></a>';
    }
    myplugin_setup_table();
}

function myplugin_setup_table () {
    $realm_id=99;
    global $config, $database_default;
    include_once($config["library_path"] . "/database.php");
    $sql = "show columns from host from " . $database_default;
    $result = mysql_query($sql) or die (mysql_error());
    $found = false;
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if ($row['Field'] == 'myplugin')
            $found = true;
    }
    if (!$found) {
        $sql = "alter table host add myplugin char(3) default 'on' not null after disabled";
        $result = mysql_query($sql) or die (mysql_error());
        $sql = "INSERT INTO `user_auth_realm` VALUES ($realm_id, 1);";
        $result = mysql_query($sql); // or die (mysql_error());
    }
}

?>

myplugin.sql
This is a SQL dump of your database (structure and data) in its initial state
example:
DROP TABLE IF EXISTS `myplugin_data`;
CREATE TABLE `myplugin_data` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rra_id` int(11) NOT NULL DEFAULT '0',
  `data_id` int(11) NOT NULL DEFAULT '0',
  `lastread` varchar(100) DEFAULT NULL,
  `host_id` int(10) DEFAULT NULL,
  `syslog_priority` int(2) DEFAULT '3',
  PRIMARY KEY  (`id`),
  KEY `rra_id` (`rra_id`)
) TYPE=MyISAM;

DROP TABLE IF EXISTS `myplugin`;
CREATE TABLE `myplugin` (
  `id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `element` varchar(50) DEFAULT NULL,
  `rra` int(11) DEFAULT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

INSERT INTO `user_auth_realm` VALUES (99, 1);

INSERT INTO settings VALUES ('myplugin_foo',1234);
INSERT INTO settings VALUES ('myplugin_bar',5678);

index.php
This is a blank php file to hide the contents of yor plugin directory.
example:
<?php
header("Location:../index.php");
?>


myplugin.php
This is where most of the functionality of your plugin will go. The filename used for this file must be unique across a whole cacti installation, if authentication is to work as expected.

tab_myplugin.png
This is the graphic for cacti's tab. You can download blank tab templates here

CategoryDeveloper
Page was generated in 0.3793 seconds