-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport.c
129 lines (110 loc) · 4.26 KB
/
report.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
+----------------------------------------------------------------------+
| hawkeye |
+----------------------------------------------------------------------+
| this source file is subject to version 2.0 of the apache license, |
| that is bundled with this package in the file license, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/license-2.0.html |
| if you did not receive a copy of the apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| author: weijun lu <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "report.h"
extern hawkeye_config_t *_config;
static char sn[128] = {0};
static int sn_getted = 0;
static int get_sn(char *sn, int size);
int report_do(hawkeye_t *hawkeye, int timeout)
{
CURL *curl = NULL;
CURLcode result = -1;
char post_buffer[8 * 1024] = {0};
int len = 0;
if (!sn_getted) {
if (!get_sn(sn, sizeof(sn))) {
log_error("get_sn failed.");
return 0;
}
sn_getted = 1;
}
curl = curl_easy_init();
if (NULL == curl) {
log_error("get curl handle failed.");
return 0;
}
len = snprintf(post_buffer, sizeof(post_buffer), "t=%lu&sn=%s&%s&%s&%s&%s&%s&%s&%s&%s",
time(0),
sn,
hawkeye->cpu_stat_json,
hawkeye->io_stat_json,
hawkeye->load_stat_json,
hawkeye->memory_stat_json,
hawkeye->nginx_stat_json,
hawkeye->partition_stat_json,
hawkeye->traffic_stat_json,
hawkeye->processes_stat_json);
if (len >= (int)sizeof(post_buffer)) {
log_error("post_buffer is too small.");
curl_easy_cleanup(curl);
return 0;
}
//printf("-------------post: len[%d], buffer: %s\n", len, post_buffer);
if (_config->proxy_server) {
/*char proxy_server[1024] = {0};
snprintf(proxy_server, sizeof(proxy_server) - 1, "%s:%d", _config->proxy_server, _config->proxy_port);
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_server);*/
curl_easy_setopt(curl, CURLOPT_PROXY, _config->proxy_server);
curl_easy_setopt(curl, CURLOPT_PROXYPORT, _config->proxy_port);
}
if (_config->proxy_user) {
char proxy_user[1024] = {0};
snprintf(proxy_user, sizeof(proxy_user) - 1, "%s:%s", _config->proxy_user, _config->proxy_password);
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user);
}
curl_easy_setopt(curl, CURLOPT_URL, _config->status_report_url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);
curl_easy_setopt(curl, CURLOPT_POST, 8);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_buffer);
result = curl_easy_perform(curl);
if (result != CURLE_OK) {
log_error("report to: %s failed, %s.", _config->status_report_url, curl_easy_strerror(result));
curl_easy_cleanup(curl);
return 0;
}
curl_easy_cleanup(curl);
return 1;
}
static int get_sn(char *sn, int size)
{
FILE *fp = NULL;
char line[1024];
fp = popen("dmidecode -t1", "r");
if (NULL == fp) {
log_error("popen dmidecode -t1 failed.");
return 0;
}
bzero(line, sizeof(line));
while (NULL != fgets(line, sizeof(line) - 1, fp)) {
char *str = NULL;
if (NULL != (str = strstr(line, "Serial Number: "))) {
strncpy(sn, str + sizeof("Serial Number:"), size - 1);
str = sn;
while (*str) {
if (*str == '\n' || *str == '\r') {
*str = 0;
break;
}
str++;
}
pclose(fp);
return 1;
}
bzero(line, sizeof(line));
}
pclose(fp);
return 0;
}