更新时是先填充一个data,然后将原先的释放,然后指向这个data
#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 "Soapaction:urn:vim25/4.1" #define ZBX_XML_HEADER2 "Content-Type:text/xml; charset=utf-8" 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) { int err, 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, "" )) || CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_FOLLOWLOCATION, 1L)) || CURLE_OK != (err = curl_easy_setopt(easyhandle, opt = CURLOPT_URL, "https://192.168.30.222/sdk")) || 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:ns0=\"urn:vim25\" xmlns:ns1=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><ns1:Body><ns0:Login xsi:type=\"ns0:LoginRequestType\"><ns0:_this type=\"SessionManager\">SessionManager</ns0:_this><ns0:userName>Administrator</ns0:userName><ns0:password>qwerASDF</ns0:password></ns0:Login></ns1:Body></SOAP-ENV:Envelope>"; curl_easy_setopt(easyhandle, opt = CURLOPT_POSTFIELDS, xml); err = curl_easy_perform(easyhandle); char *xml1; xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:ns0=\"urn:vim25\" xmlns:ns1=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><ns1:Body><ns0:RetrievePropertiesEx><ns0:_this type=\"PropertyCollector\">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=\"VirtualMachine\">vm-21</ns0:obj></ns0:objectSet></ns0:specSet><ns0:options></ns0:options></ns0:RetrievePropertiesEx></ns1:Body></SOAP-ENV:Envelope>"; curl_easy_setopt(easyhandle, opt = CURLOPT_POSTFIELDS, xml1); page.offset = 0; err = curl_easy_perform(easyhandle); if (NULL == page.data) { printf("error"); } printf("%s", page.data); clean: curl_easy_cleanup(easyhandle); return 0; }
zabbix监控vmware的数据结构、业务逻辑和最简单原型代码
原文:http://blog.csdn.net/xpx3216/article/details/19901167