Cacti 插件中setup.php 文件的编写
名词: 初始化函数 预定义函数
插件放在 /plugins 目录 由setup.php与cacti 做关联
setup.php文件由/include/plugins.php /lib/plugins.php这两个文件做解释
插件名要在/include/config.php 或/include/global.php 文件中声明
$plugins[] = ‘thold‘;
setup.php文件放置目录/plugins/插件名/setup.php
编写的文件内容由插件初始化函数(plugin_init_插件名())和自定义函数组成
function plugin_init_thold() {
global $plugin_hooks;
$plugin_hooks[‘user_admin_edit‘][‘thold‘] = ‘thold_user_admin_edit‘;
}
初始化函数声明格式:
$plugin_hooks |
[‘user_admin_edit‘] |
[‘thold‘] |
= |
‘thold_user_admin_edit‘ |
; |
|
预定义函数名 |
插件名 |
|
自定义函数 |
|
预定义函数名列表:
预定义函数名 |
插件引用函数 |
对应修改页面 |
auth_alternate_realms |
do_hook_function() |
auth.php |
login_options_navigate |
api_plugin_hook_function() |
auth_changepassword.php |
|
|
auth_login.php |
login_before |
api_plugin_hook() |
auth_login.php |
cacti_image |
api_plugin_hook_function() |
auth_login.php |
login_after |
api_plugin_hook() |
auth_login.php |
data_sources_table |
api_plugin_hook_function() |
data_sources.php |
graph_buttons |
api_plugin_hook() |
graph.php |
|
|
/lib/html.php |
graphs_action_array |
api_plugin_hook_function() |
graphs.php |
graphs_action_execute |
api_plugin_hook_function() |
graphs.php |
graphs_action_prepare |
api_plugin_hook_function() |
graphs.php |
graphs_new_top_links |
api_plugin_hook() |
graphs_new.php |
graph_image |
api_plugin_hook_function() |
graph_image.php |
device_action_array |
api_plugin_hook_function() |
host.php |
device_action_execute |
api_plugin_hook_function() |
host.php |
device_action_prepare |
api_plugin_hook_function() |
host.php |
console_before |
api_plugin_hook() |
index.php |
console_after |
api_plugin_hook() |
index.php |
poller_top |
api_plugin_hook() |
poller.php |
poller_command_args |
api_plugin_hook_function() |
poller.php |
poller_bottom |
api_plugin_hook() |
poller.php |
user_admin_action |
api_plugin_hook_function() |
user_admin.php |
user_admin_setup_sql_save |
api_plugin_hook_function() |
user_admin.php |
user_admin_user_save |
api_plugin_hook() |
user_admin.php |
user_admin_edit |
api_plugin_hook_function() |
user_admin.php |
user_admin_tab |
api_plugin_hook() |
user_admin.php |
user_admin_run_action |
api_plugin_hook_function() |
user_admin.php |
utilities_action |
api_plugin_hook_function() |
utilities.php |
utilities_list |
api_plugin_hook() |
utilities.php |
config_arrays |
api_plugin_hook() |
/include/global_arrays.php |
config_form |
api_plugin_hook() |
/include/global_form.php |
valid_host_fields |
api_plugin_hook_function() |
/include/global_form.php |
config_settings |
api_plugin_hook() |
/include/global_settings.php |
top_graph_header |
api_plugin_hook_function() |
/include/top_graph_header.php |
page_title |
api_plugin_hook_function() |
/include/top_graph_header.php |
|
|
/include/top_header.php |
top_graph_refresh |
api_plugin_hook_function() |
/include/top_graph_header.php |
page_head |
api_plugin_hook() |
/include/top_graph_header.php |
|
|
/include/top_header.php |
top_graph_header_tabs |
api_plugin_hook() |
/include/top_graph_header.php |
top_header |
api_plugin_hook_function() |
/include/top_header.php |
top_header_tabs |
api_plugin_hook() |
/include/top_header.php |
api_device_save |
api_plugin_hook_function() |
/lib/api_device.php |
draw_navigation_tex |
api_plugin_hook_function() |
/lib/functions.php |
poller_output |
api_plugin_hook_function() |
/lib/poller.php |
poller_on_demand |
api_plugin_hook_function() |
/lib/poller.php |
rrdtool_function_graph_cache_check |
api_plugin_hook_function() |
/lib/rrd.php |
rrd_graph_graph_options |
api_plugin_hook_function() |
/lib/rrd.php |
prep_graph_array |
api_plugin_hook_function() |
/lib/rrd.php |
rrdtool_function_graph_set_file |
api_plugin_hook_function() |
/lib/rrd.php |
substitute_host_data |
api_plugin_hook_function() |
/lib/variables.php |
自定义函数举例:
function thold_user_admin_edit ($user) {
global $fields_user_user_edit_host;
$value = ‘‘;
if ($user != 0) {
$value = db_fetch_cell("SELECT data FROM plugin_thold_contacts WHERE user_id
= $user AND type = ‘email‘");
}
$fields_user_user_edit_host[‘email‘] = array(
"method" => "textbox",
"value" => $value,
"friendly_name" => "电子邮件地址",
"form_id" => "|arg1:id|",
"default" => "",
"max_length" => 255
);
}
最近公司要在cacti的基础上做一点开发,就是报警系统。在cactiez中,只有一种报警声音---宕机,就是只有宕机的时候才会报警。这显然不够,我们需要在内存超出阈值,CPU使用率超出阈值,硬盘使用空间超出阈值等等等等情况出现时都报警,而且报警声音都不一样。
实现这一功能涉及到的文件有/plugins/monitor文件夹下的setup.php和monitor.php两个文件。比如,如果我们想要当内存超出设置的阈值时就发出一种报警声音,这里的报警声音我假设为memory-sound.mp3.那么先在setup.php 文件下把这个内存报警和声音定义好.在functions monitor_config_settings()这个方法的$temp数组下,添加如下一个数组(‘最好添加在monitor_sound下面’)
$temp = array(
‘monitor_header’ => array(…….),
‘monitor_sound’ => array(………),
‘memory_sound‘ => array(
‘friendly_name‘ => ‘内存超出报警声音‘,
‘method‘ => ‘drop_array‘,
‘array‘ => monitor_scan_dir(),
‘default‘ => ‘memory-sound.mp3‘,
)
……….
)
其中, friendly_name的作用是,当你添加这个时,在”设置à杂项à里面就会出现”如下所示的选项:
仅仅这样做是不够的,这里只是告诉系统,我要有内存警报这个功能,至于这个功能怎么实现,声音怎么发出来,我们还需要自己现实。这个实现就涉及到monitor.php文件和thold_data表。
在说明怎么实现前,需要先说一下thold_data表。这个表是存储阈值用的,表里面有个thold_alert字段,当监控的指标超出阈值时,这个字段就会变成非0数字(我这里是数字2),一旦个数字不是0,说明有监控项超出阈值。
图中的1表示开启阈值功能,二 表示的监控项是否超过阈值,如果超过的话就会变成非0,否则为0(至于怎么把采集的数据和阈值比较则不是我们需要掌握的),三 表示的是阈值上限(我没设下限),四 是监控项的名字,这里有点乱码,不过大家应该能看到网卡流量和CPU。
在实现内存警报的功能之前,首先要从thold_data这个数据库把thold_alert的值查出来,然后查看内存对应的thold_alert是多少,是0的话就没事,非0的话就到monitor.php中去实现发出警报声的功能。
发出警报声:
在monitor.php中,实现发出警报声的是如下这段代码:
if ($host_down && $sound) {
$monitor_sound = read_config_option(‘monitor_sound‘);
if ($monitor_sound != ‘‘ && $monitor_sound != ‘None‘) {
print ‘<EMBED src=\‘#\‘" /monitor/sounds/‘ . $monitor_sound . ‘" autostart=true loop=true volume=100 hidden=true><NOEMBED><BGSOUND src=\‘#\‘" /monitor/sounds/‘ . $monitor_sound . ‘"></NOEMBED>‘ . "\n";
}
}
这里是宕机的警报声。我们可以修改一下
先从thold_data中查出memory对应的thold_alert的值,设为memory_thold_alert,然后
If($sound){//把sound的检测提到外面,这样有利于扩展
//如果查到的数据非0,当然前面还有这个值的查询代码,略过
If($memory_thold_alert){
//去setup.php(刚才设置过的)文件读取内存对应的警报声
$memory_sound = read_config_option(‘memory_sound‘);
//如果警报声非空,那么播放声音,播放声音是通过一个html标签实现的,简单
if ($memory_sound != ‘‘ && $memory_sound != ‘None‘) {
print ‘<EMBED src=\‘#\‘" /monitor/sounds/‘ . $memory_sound . ‘" autostart=true loop=true volume=100 hidden=true><NOEMBED><BGSOUND src=\‘#\‘" /monitor/sounds/‘ . $memory_sound . ‘"></NOEMBED>‘ . "\n";
}
}
如果还想对应其他的监控项发出不同的警报声,那么,同上。