@@ -168,6 +168,7 @@ sum_data_hoster()
168
168
struct item_data * target = NULL ;
169
169
struct item_data * ch ;
170
170
struct item_data * next_ch ;
171
+ const char * hostname = getenv ("HOST" );
171
172
char sql [512 ];
172
173
char stats_file [1024 ];
173
174
int ret = 0 ;
@@ -189,6 +190,13 @@ sum_data_hoster()
189
190
gettimeofday (& now_time , NULL );
190
191
cur_time = (time_t )now_time .tv_sec ;
191
192
193
+ // First, free existing sum_item_list
194
+ for (sumch = sum_item_list ; sumch ; sumch = next_sumch ) {
195
+ next_sumch = sumch -> next ;
196
+ free (sumch );
197
+ }
198
+ sum_item_list = NULL ;
199
+
192
200
for (ch = item_list ; ch ; ch = ch -> next ) {
193
201
if (ch -> modified == 0 ) {
194
202
continue ;
@@ -207,26 +215,22 @@ sum_data_hoster()
207
215
sumch -> memoryuse += ch -> memoryuse ;
208
216
sumch -> maxproc += ch -> maxproc ;
209
217
sumch -> openfiles += ch -> openfiles ;
210
- // sumch->readbps+=ch->readbps;
211
- // sumch->writebps+=ch->writebps;
212
- // sumch->readiops+=ch->readiops;
213
- // sumch->writeiops+=ch->writeiops;
214
218
sumch -> temperature += ch -> temperature ;
215
219
sumch -> pmem += ch -> pmem ;
216
220
break ;
217
221
}
218
222
}
219
223
} else {
220
224
CREATE (newd , struct sum_item_data , 1 );
225
+ if (!newd ) {
226
+ tolog (log_level , "Failed to allocate memory for newd\n" );
227
+ continue ;
228
+ }
221
229
newd -> modified = ch -> modified ;
222
230
newd -> pcpu = ch -> pcpu ;
223
231
newd -> memoryuse = ch -> memoryuse ;
224
232
newd -> maxproc = ch -> maxproc ;
225
233
newd -> openfiles = ch -> openfiles ;
226
- // newd->readbps=ch->readbps;
227
- // newd->writebps=ch->writebps;
228
- // newd->readiops=ch->readiops;
229
- // newd->writeiops=ch->writeiops;
230
234
newd -> temperature = ch -> temperature ;
231
235
newd -> pmem = ch -> pmem ;
232
236
newd -> next = sum_item_list ;
@@ -249,14 +253,19 @@ sum_data_hoster()
249
253
sumch -> modified / round_total );
250
254
if (OUTPUT_BEANSTALKD & output_flags ) {
251
255
memset (json_buf , 0 , sizeof (json_buf ));
252
- sprintf (json_buf ,
256
+ snprintf (json_buf , sizeof ( json_buf ) ,
253
257
"{\"name\": \"%s\",\"time\": %d,\"pcpu\": %d,\"pmem\": %d }" ,
254
258
sumch -> name , cur_time , sumch -> pcpu / round_total ,
255
259
sumch -> pmem / round_total );
256
260
257
261
if (strlen (json_str ) > 2 ) {
258
- strcat (json_str , "," );
259
- strcat (json_str , json_buf );
262
+ if (strlen (json_str ) + strlen (json_buf ) + 2 < sizeof (json_str )) {
263
+ strcat (json_str , "," );
264
+ strcat (json_str , json_buf );
265
+ } else {
266
+ tolog (log_level , "Buffer overflow in json_str\n" );
267
+ break ;
268
+ }
260
269
} else {
261
270
strcpy (json_str ,
262
271
"{ \"tube\":\"racct-system\", \"node\":\"clonos.convectix.com\", \"data\":[" );
@@ -266,8 +275,8 @@ sum_data_hoster()
266
275
267
276
#ifdef WITH_INFLUX
268
277
if (OUTPUT_INFLUX & output_flags ) {
269
-
270
- sprintf (influx -> buffer + strlen (influx -> buffer ),
278
+ snprintf ( influx -> buffer + strlen ( influx -> buffer ),
279
+ sizeof (influx -> buffer ) - strlen (influx -> buffer ),
271
280
"%s,node=%s,host=%s%s%s memoryuse=%lu,maxproc=%d,openfiles=%d,pcpu=%d,pmem=%d,temperature=%2.2f %lu\n" ,
272
281
influx -> tables .nodes , nodename , sumch -> name ,
273
282
(influx -> tags .nodes == NULL ? "" : "," ),
@@ -280,22 +289,7 @@ sum_data_hoster()
280
289
sumch -> pmem / round_total ,
281
290
sumch -> temperature / round_total , nanoseconds ());
282
291
283
- /*
284
- printf("%s,node=%s,host=%s%s%s
285
- memoryuse=%lu,maxproc=%d,openfiles=%d,pcpu=%d,pmem=%d,temperature=%2.2f
286
- %lu\n", influx->tables.nodes, nodename, sumch->name,
287
- (influx->tags.nodes==NULL?"":","),
288
- (influx->tags.nodes==NULL?"":influx->tags.nodes),
289
- sumch->memoryuse/round_total,
290
- sumch->maxproc/round_total,
291
- sumch->openfiles/round_total,
292
- sumch->pcpu/round_total,
293
- sumch->pmem/round_total,sumch->temperature/round_total,
294
- nanoseconds());
295
- */
296
292
influx -> items ++ ;
297
- // tolog(log_level,"%d RACCT items
298
- // queued for storage\n", influx->items);
299
293
}
300
294
#endif
301
295
#ifdef WITH_REDIS
@@ -305,24 +299,21 @@ sum_data_hoster()
305
299
if (OUTPUT_SQLITE3 & output_flags ) {
306
300
memset (sql , 0 , sizeof (sql ));
307
301
memset (stats_file , 0 , sizeof (stats_file ));
308
- sprintf (stats_file , "%s/jails-system/%s/racct.sqlite" ,
302
+ snprintf (stats_file , sizeof ( stats_file ) , "%s/jails-system/%s/racct.sqlite" ,
309
303
workdir , sumch -> name );
310
304
fp = fopen (stats_file , "r" );
311
305
if (!fp ) {
312
306
tolog (log_level ,
313
307
"RACCT not exist, create via updatesql\n" );
314
- sprintf (sql ,
308
+ snprintf (sql , sizeof ( sql ) ,
315
309
"/usr/local/bin/cbsd /usr/local/cbsd/misc/updatesql %s /usr/local/cbsd/share/racct.schema racct" ,
316
310
stats_file );
317
311
system (sql );
318
- // write into base in next loop (protection if
319
- // jail was removed in directory not exist
320
- // anymore
321
312
continue ;
322
313
}
323
314
fclose (fp );
324
315
325
- sprintf (sql ,
316
+ snprintf (sql , sizeof ( sql ) ,
326
317
"INSERT INTO racct ( idx,memoryuse,maxproc,openfiles,pcpu,pmem ) VALUES ( '%d', '%lu', '%d', '%d', '%d', '%d' );\n" ,
327
318
cur_time , sumch -> memoryuse / round_total ,
328
319
sumch -> maxproc / round_total ,
@@ -338,18 +329,19 @@ sum_data_hoster()
338
329
sumch -> memoryuse = 0 ;
339
330
sumch -> maxproc = 0 ;
340
331
sumch -> openfiles = 0 ;
341
- // sumch->readbps=0;
342
- // sumch->writebps=0;
343
- // sumch->readiops=0;
344
- // sumch->writeiops=0;
345
332
sumch -> temperature = 0 ;
346
333
sumch -> pmem = 0 ;
347
334
348
335
remove_data_by_jname (sumch -> name );
349
336
}
350
337
351
338
if (OUTPUT_BEANSTALKD & output_flags ) {
352
- strcat (json_str , "]}" );
339
+ if (strlen (json_str ) + 2 < sizeof (json_str )) {
340
+ strcat (json_str , "]}" );
341
+ } else {
342
+ tolog (log_level , "Buffer overflow in json_str\n" );
343
+ skip_beanstalk = 1 ;
344
+ }
353
345
bs_tick = 0 ;
354
346
}
355
347
0 commit comments