Most recent edit on 2006-03-13 07:46:09 by HowardJones [Added a note about filenames and authentication.]

Additions:
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.

Deletions:
This is where most of the functionality of your plugin will go.



Edited on 2006-02-19 03:33:13 by PsybeR [updated function myplugin_version to support update plugin]

Additions:
'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'
);

Deletions:
'version' => '0.1b');



Edited on 2006-01-30 11:33:17 by PsybeR

Additions:
CategoryDeveloper

Deletions:
CatagoryDeveloper



Edited on 2006-01-30 11:32:20 by PsybeR

Additions:
CatagoryDeveloper

Deletions:
The following 1 pages belong to PluginStructure:

Homepage




Edited on 2006-01-30 11:31:15 by PsybeR

Additions:
The following 1 pages belong to PluginStructure:

Homepage




Edited on 2006-01-30 11:21:55 by PsybeR

Additions:
~~~& index.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:
This is where you hook into the plugin archetecture
This is a SQL dump of your database (structure and data) in its initial state
example:
%%(sql)
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);
This is a blank php file to hide the contents of yor plugin directory.
example:
header("Location:../index.php");
This is where most of the functionality of your plugin will go.
This is the graphic for cacti's tab. You can download blank tab templates here


Deletions:
This file sets up all your funtions that hook into cacti
structure:




Oldest known version of this page was edited on 2006-01-30 10:43:40 by PsybeR []
Page view:
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
      • tab_myplugin.png

Files

setup.php
This file sets up all your funtions that hook into cacti
structure:
<?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() {
    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.1b');
}

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

index.php

myplugin.php

tab_myplugin.png
Page was generated in 0.4080 seconds