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

[经验分享] zabbix监控vmware的数据结构、业务逻辑和最简单原型代码

[复制链接]

尚未签到

发表于 2015-11-23 12:40:37 | 显示全部楼层 |阅读模式
  

一、数据结构:
DSC0000.jpg
  

二、业务逻辑

什么时候什么条件下更新?
    zbx_vmware_init的时候初始化了zbx_vmware_t的实例vmware和其唯一的属性vector services


    zbx_vmware_loop处理vmware->services列表中的service,间隔5s
        如果service的now-lastaccess > 1天
            从services中删除该service
        如果service的now-lastcheck >= vmware更新间隔
            标记state为更新


        如果state为更新则调用vmware_service_update来更新


vmware->services的内容怎么来的呢?
    checks_simple_vmware.c中get_vmware_service调用vmware.c中的zbx_vmware_get_service
    从vmware->services中查找url,username和password相同的,如果已经有数据则返回,没有数据则返回null
    如果vmware->services中没有,则添加  
  更新时是先填充一个data,然后将原先的释放,然后指向这个data
  

三、最简单原型代码
  编译: gcc simple.c -lcurl

#include <curl/curl.h>
typedef struct
{
char    *data;
size_t  alloc;
size_t  offset;
}
ZBX_HTTPPAGE;
static ZBX_HTTPPAGE     page;
#define zbx_malloc(old, size)           zbx_malloc2(__FILE__, __LINE__, old, size)
#define zbx_realloc(src, size)          zbx_realloc2(__FILE__, __LINE__, src, size)
#define ZBX_XML_HEADER1         &quot;Soapaction:urn:vim25/4.1&quot;
#define ZBX_XML_HEADER2         &quot;Content-Type:text/xml; charset=utf-8&quot;
void    *zbx_malloc2(const char *filename, int line, void *old, size_t size)
{
int     max_attempts;
void    *ptr = NULL;
/* old pointer must be NULL */
if (NULL != old)
{
}
for (
max_attempts = 10, size = size;
0 < max_attempts && NULL == ptr;
ptr = malloc(size), max_attempts--
);
if (NULL != ptr)
return ptr;
exit(-1);
}
void    *zbx_realloc2(const char *filename, int line, void *old, size_t size)
{
int     max_attempts;
void    *ptr = NULL;
for (
max_attempts = 10;
0 < max_attempts && NULL == ptr;
ptr = realloc(old, size), max_attempts--
);
if (NULL != ptr)
return ptr;
exit(-1);
}
void    zbx_strncpy_alloc(char **str, size_t *alloc_len, size_t *offset, const char *src, size_t n)
{
if (NULL == *str)
{
*alloc_len = n + 1;
*offset = 0;
*str = zbx_malloc(*str, *alloc_len);
}
else if (*offset + n >= *alloc_len)
{
while (*offset + n >= *alloc_len)
*alloc_len *= 2;
*str = zbx_realloc(*str, *alloc_len);
}
while (0 != n && '\0' != *src)
{
(*str)[(*offset)++] = *src++;
n--;
}
(*str)[*offset] = '\0';
}
static size_t   WRITEFUNCTION2(void *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t  r_size = size * nmemb;
zbx_strncpy_alloc(&page.data, &page.alloc, &page.offset, ptr, r_size);
return r_size;
}
static size_t   HEADERFUNCTION2(void *ptr, size_t size, size_t nmemb, void *userdata)
{
return size * nmemb;
}
int main(void)
{
interr, opt;
CURL            *easyhandle;
struct curl_slist       *headers = NULL;
if (NULL == (easyhandle = curl_easy_init()))
{
goto clean;
}
headers = curl_slist_append(headers, ZBX_XML_HEADER1);
headers = curl_slist_append(headers, ZBX_XML_HEADER2);
curl_easy_setopt(easyhandle, opt = CURLOPT_HTTPHEADER, headers);
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_COOKIEFILE, &quot;&quot; )) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_FOLLOWLOCATION, 1L)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_URL, &quot;https://192.168.30.222/sdk&quot;)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_WRITEFUNCTION, WRITEFUNCTION2)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_HEADERFUNCTION, HEADERFUNCTION2)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_POST, 1L)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_SSL_VERIFYPEER, 0L)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_SSL_VERIFYHOST, 0L)))
{
goto clean;
}
char * xml;
xml = &quot;<?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?><SOAP-ENV:Envelope xmlns:ns0=\&quot;urn:vim25\&quot; xmlns:ns1=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot; xmlns:SOAP-ENV=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot;><SOAP-ENV:Header/><ns1:Body><ns0:Login xsi:type=\&quot;ns0:LoginRequestType\&quot;><ns0:_this type=\&quot;SessionManager\&quot;>SessionManager</ns0:_this><ns0:userName>Administrator</ns0:userName><ns0:password>qwerASDF</ns0:password></ns0:Login></ns1:Body></SOAP-ENV:Envelope>&quot;;
curl_easy_setopt(easyhandle, opt = CURLOPT_POSTFIELDS, xml);
err = curl_easy_perform(easyhandle);
char *xml1;
xml1 = &quot;<?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?><SOAP-ENV:Envelope xmlns:ns0=\&quot;urn:vim25\&quot; xmlns:ns1=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot; xmlns:SOAP-ENV=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot;><SOAP-ENV:Header/><ns1:Body><ns0:RetrievePropertiesEx><ns0:_this type=\&quot;PropertyCollector\&quot;>propertyCollector</ns0:_this><ns0:specSet><ns0:propSet><ns0:type>VirtualMachine</ns0:type><ns0:pathSet>config</ns0:pathSet><ns0:pathSet>summary</ns0:pathSet><ns0:pathSet>guest</ns0:pathSet></ns0:propSet><ns0:objectSet><ns0:obj type=\&quot;VirtualMachine\&quot;>vm-21</ns0:obj></ns0:objectSet></ns0:specSet><ns0:options></ns0:options></ns0:RetrievePropertiesEx></ns1:Body></SOAP-ENV:Envelope>&quot;;
curl_easy_setopt(easyhandle, opt = CURLOPT_POSTFIELDS, xml1);
page.offset = 0;
err = curl_easy_perform(easyhandle);
if (NULL == page.data) {
printf(&quot;error&quot;);
}
printf(&quot;%s&quot;, page.data);
clean:
curl_easy_cleanup(easyhandle);
return 0;
}

  

如果不想编码,也可以,直接用curl -i -k --data &quot;&quot;
https://192.168.30.222/sdk 也行,只要data里的内容填对了,就可以。

运维网声明 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-142629-1-1.html 上篇帖子: 安装zabbix客户端脚本 下篇帖子: Nothing to be done for `install-exec-am' while compile zabbix
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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