chenkehao 发表于 2015-11-23 12:40:37

zabbix监控vmware的数据结构、业务逻辑和最简单原型代码

  

一、数据结构:

  

二、业务逻辑

什么时候什么条件下更新?
    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_talloc;
size_toffset;
}
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_tr_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]
查看完整版本: zabbix监控vmware的数据结构、业务逻辑和最简单原型代码