设为首页 收藏本站
查看: 1969|回复: 0

[经验分享] Cactiez二次开发---警报添加

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-27 08:27:37 | 显示全部楼层 |阅读模式
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的作用是,当你添加这个时,在”设置à杂项à里面就会出现”如下所示的选项:
spacer.jpg
仅仅这样做是不够的,这里只是告诉系统,我要有内存警报这个功能,至于这个功能怎么实现,声音怎么发出来,我们还需要自己现实。这个实现就涉及到monitor.php文件和thold_data表。
在说明怎么实现前,需要先说一下thold_data表。这个表是存储阈值用的,表里面有个thold_alert字段,当监控的指标超出阈值时,这个字段就会变成非0数字(我这里是数字2),一旦个数字不是0,说明有监控项超出阈值。

spacer.jpg
图中的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";
}
}
如果还想对应其他的监控项发出不同的警报声,那么,同上。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-33812-1-1.html 上篇帖子: Cacti监控MySQL多实例 下篇帖子: Linux 判断Cacti模板的版本 开发
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表