log.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <daemon/main.h>
  3. #include "../libnetdata.h"
  4. #ifdef HAVE_BACKTRACE
  5. #include <execinfo.h>
  6. #endif
  7. int web_server_is_multithreaded = 1;
  8. const char *program_name = "";
  9. uint64_t debug_flags = 0;
  10. int access_log_syslog = 1;
  11. int error_log_syslog = 1;
  12. int collector_log_syslog = 1;
  13. int output_log_syslog = 1; // debug log
  14. int health_log_syslog = 1;
  15. int stdaccess_fd = -1;
  16. FILE *stdaccess = NULL;
  17. int stdhealth_fd = -1;
  18. FILE *stdhealth = NULL;
  19. int stdcollector_fd = -1;
  20. FILE *stderror = NULL;
  21. const char *stdaccess_filename = NULL;
  22. const char *stderr_filename = NULL;
  23. const char *stdout_filename = NULL;
  24. const char *facility_log = NULL;
  25. const char *stdhealth_filename = NULL;
  26. const char *stdcollector_filename = NULL;
  27. #ifdef ENABLE_ACLK
  28. const char *aclklog_filename = NULL;
  29. int aclklog_fd = -1;
  30. FILE *aclklog = NULL;
  31. int aclklog_syslog = 1;
  32. int aclklog_enabled = 0;
  33. #endif
  34. // ----------------------------------------------------------------------------
  35. // Log facility(https://tools.ietf.org/html/rfc5424)
  36. //
  37. // The facilities accepted in the Netdata are in according with the following
  38. // header files for their respective operating system:
  39. // sys/syslog.h (Linux )
  40. // sys/sys/syslog.h (FreeBSD)
  41. // bsd/sys/syslog.h (darwin-xnu)
  42. #define LOG_AUTH_KEY "auth"
  43. #define LOG_AUTHPRIV_KEY "authpriv"
  44. #ifdef __FreeBSD__
  45. # define LOG_CONSOLE_KEY "console"
  46. #endif
  47. #define LOG_CRON_KEY "cron"
  48. #define LOG_DAEMON_KEY "daemon"
  49. #define LOG_FTP_KEY "ftp"
  50. #ifdef __APPLE__
  51. # define LOG_INSTALL_KEY "install"
  52. #endif
  53. #define LOG_KERN_KEY "kern"
  54. #define LOG_LPR_KEY "lpr"
  55. #define LOG_MAIL_KEY "mail"
  56. //#define LOG_INTERNAL_MARK_KEY "mark"
  57. #ifdef __APPLE__
  58. # define LOG_NETINFO_KEY "netinfo"
  59. # define LOG_RAS_KEY "ras"
  60. # define LOG_REMOTEAUTH_KEY "remoteauth"
  61. #endif
  62. #define LOG_NEWS_KEY "news"
  63. #ifdef __FreeBSD__
  64. # define LOG_NTP_KEY "ntp"
  65. #endif
  66. #define LOG_SECURITY_KEY "security"
  67. #define LOG_SYSLOG_KEY "syslog"
  68. #define LOG_USER_KEY "user"
  69. #define LOG_UUCP_KEY "uucp"
  70. #ifdef __APPLE__
  71. # define LOG_LAUNCHD_KEY "launchd"
  72. #endif
  73. #define LOG_LOCAL0_KEY "local0"
  74. #define LOG_LOCAL1_KEY "local1"
  75. #define LOG_LOCAL2_KEY "local2"
  76. #define LOG_LOCAL3_KEY "local3"
  77. #define LOG_LOCAL4_KEY "local4"
  78. #define LOG_LOCAL5_KEY "local5"
  79. #define LOG_LOCAL6_KEY "local6"
  80. #define LOG_LOCAL7_KEY "local7"
  81. static int log_facility_id(const char *facility_name)
  82. {
  83. static int
  84. hash_auth = 0,
  85. hash_authpriv = 0,
  86. #ifdef __FreeBSD__
  87. hash_console = 0,
  88. #endif
  89. hash_cron = 0,
  90. hash_daemon = 0,
  91. hash_ftp = 0,
  92. #ifdef __APPLE__
  93. hash_install = 0,
  94. #endif
  95. hash_kern = 0,
  96. hash_lpr = 0,
  97. hash_mail = 0,
  98. // hash_mark = 0,
  99. #ifdef __APPLE__
  100. hash_netinfo = 0,
  101. hash_ras = 0,
  102. hash_remoteauth = 0,
  103. #endif
  104. hash_news = 0,
  105. #ifdef __FreeBSD__
  106. hash_ntp = 0,
  107. #endif
  108. hash_security = 0,
  109. hash_syslog = 0,
  110. hash_user = 0,
  111. hash_uucp = 0,
  112. #ifdef __APPLE__
  113. hash_launchd = 0,
  114. #endif
  115. hash_local0 = 0,
  116. hash_local1 = 0,
  117. hash_local2 = 0,
  118. hash_local3 = 0,
  119. hash_local4 = 0,
  120. hash_local5 = 0,
  121. hash_local6 = 0,
  122. hash_local7 = 0;
  123. if(unlikely(!hash_auth))
  124. {
  125. hash_auth = simple_hash(LOG_AUTH_KEY);
  126. hash_authpriv = simple_hash(LOG_AUTHPRIV_KEY);
  127. #ifdef __FreeBSD__
  128. hash_console = simple_hash(LOG_CONSOLE_KEY);
  129. #endif
  130. hash_cron = simple_hash(LOG_CRON_KEY);
  131. hash_daemon = simple_hash(LOG_DAEMON_KEY);
  132. hash_ftp = simple_hash(LOG_FTP_KEY);
  133. #ifdef __APPLE__
  134. hash_install = simple_hash(LOG_INSTALL_KEY);
  135. #endif
  136. hash_kern = simple_hash(LOG_KERN_KEY);
  137. hash_lpr = simple_hash(LOG_LPR_KEY);
  138. hash_mail = simple_hash(LOG_MAIL_KEY);
  139. // hash_mark = simple_uhash();
  140. #ifdef __APPLE__
  141. hash_netinfo = simple_hash(LOG_NETINFO_KEY);
  142. hash_ras = simple_hash(LOG_RAS_KEY);
  143. hash_remoteauth = simple_hash(LOG_REMOTEAUTH_KEY);
  144. #endif
  145. hash_news = simple_hash(LOG_NEWS_KEY);
  146. #ifdef __FreeBSD__
  147. hash_ntp = simple_hash(LOG_NTP_KEY);
  148. #endif
  149. hash_security = simple_hash(LOG_SECURITY_KEY);
  150. hash_syslog = simple_hash(LOG_SYSLOG_KEY);
  151. hash_user = simple_hash(LOG_USER_KEY);
  152. hash_uucp = simple_hash(LOG_UUCP_KEY);
  153. #ifdef __APPLE__
  154. hash_launchd = simple_hash(LOG_LAUNCHD_KEY);
  155. #endif
  156. hash_local0 = simple_hash(LOG_LOCAL0_KEY);
  157. hash_local1 = simple_hash(LOG_LOCAL1_KEY);
  158. hash_local2 = simple_hash(LOG_LOCAL2_KEY);
  159. hash_local3 = simple_hash(LOG_LOCAL3_KEY);
  160. hash_local4 = simple_hash(LOG_LOCAL4_KEY);
  161. hash_local5 = simple_hash(LOG_LOCAL5_KEY);
  162. hash_local6 = simple_hash(LOG_LOCAL6_KEY);
  163. hash_local7 = simple_hash(LOG_LOCAL7_KEY);
  164. }
  165. int hash = simple_hash(facility_name);
  166. if ( hash == hash_auth )
  167. {
  168. return LOG_AUTH;
  169. }
  170. else if ( hash == hash_authpriv )
  171. {
  172. return LOG_AUTHPRIV;
  173. }
  174. #ifdef __FreeBSD__
  175. else if ( hash == hash_console )
  176. {
  177. return LOG_CONSOLE;
  178. }
  179. #endif
  180. else if ( hash == hash_cron )
  181. {
  182. return LOG_CRON;
  183. }
  184. else if ( hash == hash_daemon )
  185. {
  186. return LOG_DAEMON;
  187. }
  188. else if ( hash == hash_ftp )
  189. {
  190. return LOG_FTP;
  191. }
  192. #ifdef __APPLE__
  193. else if ( hash == hash_install )
  194. {
  195. return LOG_INSTALL;
  196. }
  197. #endif
  198. else if ( hash == hash_kern )
  199. {
  200. return LOG_KERN;
  201. }
  202. else if ( hash == hash_lpr )
  203. {
  204. return LOG_LPR;
  205. }
  206. else if ( hash == hash_mail )
  207. {
  208. return LOG_MAIL;
  209. }
  210. /*
  211. else if ( hash == hash_mark )
  212. {
  213. //this is internal for all OS
  214. return INTERNAL_MARK;
  215. }
  216. */
  217. #ifdef __APPLE__
  218. else if ( hash == hash_netinfo )
  219. {
  220. return LOG_NETINFO;
  221. }
  222. else if ( hash == hash_ras )
  223. {
  224. return LOG_RAS;
  225. }
  226. else if ( hash == hash_remoteauth )
  227. {
  228. return LOG_REMOTEAUTH;
  229. }
  230. #endif
  231. else if ( hash == hash_news )
  232. {
  233. return LOG_NEWS;
  234. }
  235. #ifdef __FreeBSD__
  236. else if ( hash == hash_ntp )
  237. {
  238. return LOG_NTP;
  239. }
  240. #endif
  241. else if ( hash == hash_security )
  242. {
  243. //FreeBSD is the unique that does not consider
  244. //this facility deprecated. We are keeping
  245. //it for other OS while they are kept in their headers.
  246. #ifdef __FreeBSD__
  247. return LOG_SECURITY;
  248. #else
  249. return LOG_AUTH;
  250. #endif
  251. }
  252. else if ( hash == hash_syslog )
  253. {
  254. return LOG_SYSLOG;
  255. }
  256. else if ( hash == hash_user )
  257. {
  258. return LOG_USER;
  259. }
  260. else if ( hash == hash_uucp )
  261. {
  262. return LOG_UUCP;
  263. }
  264. else if ( hash == hash_local0 )
  265. {
  266. return LOG_LOCAL0;
  267. }
  268. else if ( hash == hash_local1 )
  269. {
  270. return LOG_LOCAL1;
  271. }
  272. else if ( hash == hash_local2 )
  273. {
  274. return LOG_LOCAL2;
  275. }
  276. else if ( hash == hash_local3 )
  277. {
  278. return LOG_LOCAL3;
  279. }
  280. else if ( hash == hash_local4 )
  281. {
  282. return LOG_LOCAL4;
  283. }
  284. else if ( hash == hash_local5 )
  285. {
  286. return LOG_LOCAL5;
  287. }
  288. else if ( hash == hash_local6 )
  289. {
  290. return LOG_LOCAL6;
  291. }
  292. else if ( hash == hash_local7 )
  293. {
  294. return LOG_LOCAL7;
  295. }
  296. #ifdef __APPLE__
  297. else if ( hash == hash_launchd )
  298. {
  299. return LOG_LAUNCHD;
  300. }
  301. #endif
  302. return LOG_DAEMON;
  303. }
  304. //we do not need to use this now, but I already created this function to be
  305. //used case necessary.
  306. /*
  307. char *log_facility_name(int code)
  308. {
  309. char *defvalue = { "daemon" };
  310. switch(code)
  311. {
  312. case LOG_AUTH:
  313. {
  314. return "auth";
  315. }
  316. case LOG_AUTHPRIV:
  317. {
  318. return "authpriv";
  319. }
  320. #ifdef __FreeBSD__
  321. case LOG_CONSOLE:
  322. {
  323. return "console";
  324. }
  325. #endif
  326. case LOG_CRON:
  327. {
  328. return "cron";
  329. }
  330. case LOG_DAEMON:
  331. {
  332. return defvalue;
  333. }
  334. case LOG_FTP:
  335. {
  336. return "ftp";
  337. }
  338. #ifdef __APPLE__
  339. case LOG_INSTALL:
  340. {
  341. return "install";
  342. }
  343. #endif
  344. case LOG_KERN:
  345. {
  346. return "kern";
  347. }
  348. case LOG_LPR:
  349. {
  350. return "lpr";
  351. }
  352. case LOG_MAIL:
  353. {
  354. return "mail";
  355. }
  356. #ifdef __APPLE__
  357. case LOG_NETINFO:
  358. {
  359. return "netinfo" ;
  360. }
  361. case LOG_RAS:
  362. {
  363. return "ras";
  364. }
  365. case LOG_REMOTEAUTH:
  366. {
  367. return "remoteauth";
  368. }
  369. #endif
  370. case LOG_NEWS:
  371. {
  372. return "news";
  373. }
  374. #ifdef __FreeBSD__
  375. case LOG_NTP:
  376. {
  377. return "ntp" ;
  378. }
  379. case LOG_SECURITY:
  380. {
  381. return "security";
  382. }
  383. #endif
  384. case LOG_SYSLOG:
  385. {
  386. return "syslog";
  387. }
  388. case LOG_USER:
  389. {
  390. return "user";
  391. }
  392. case LOG_UUCP:
  393. {
  394. return "uucp";
  395. }
  396. case LOG_LOCAL0:
  397. {
  398. return "local0";
  399. }
  400. case LOG_LOCAL1:
  401. {
  402. return "local1";
  403. }
  404. case LOG_LOCAL2:
  405. {
  406. return "local2";
  407. }
  408. case LOG_LOCAL3:
  409. {
  410. return "local3";
  411. }
  412. case LOG_LOCAL4:
  413. {
  414. return "local4" ;
  415. }
  416. case LOG_LOCAL5:
  417. {
  418. return "local5";
  419. }
  420. case LOG_LOCAL6:
  421. {
  422. return "local6";
  423. }
  424. case LOG_LOCAL7:
  425. {
  426. return "local7" ;
  427. }
  428. #ifdef __APPLE__
  429. case LOG_LAUNCHD:
  430. {
  431. return "launchd";
  432. }
  433. #endif
  434. }
  435. return defvalue;
  436. }
  437. */
  438. // ----------------------------------------------------------------------------
  439. void syslog_init() {
  440. static int i = 0;
  441. if(!i) {
  442. openlog(program_name, LOG_PID,log_facility_id(facility_log));
  443. i = 1;
  444. }
  445. }
  446. void log_date(char *buffer, size_t len, time_t now) {
  447. if(unlikely(!buffer || !len))
  448. return;
  449. time_t t = now;
  450. struct tm *tmp, tmbuf;
  451. tmp = localtime_r(&t, &tmbuf);
  452. if (tmp == NULL) {
  453. buffer[0] = '\0';
  454. return;
  455. }
  456. if (unlikely(strftime(buffer, len, "%Y-%m-%d %H:%M:%S", tmp) == 0))
  457. buffer[0] = '\0';
  458. buffer[len - 1] = '\0';
  459. }
  460. static netdata_mutex_t log_mutex = NETDATA_MUTEX_INITIALIZER;
  461. static inline void log_lock() {
  462. netdata_mutex_lock(&log_mutex);
  463. }
  464. static inline void log_unlock() {
  465. netdata_mutex_unlock(&log_mutex);
  466. }
  467. static FILE *open_log_file(int fd, FILE *fp, const char *filename, int *enabled_syslog, int is_stdaccess, int *fd_ptr) {
  468. int f, devnull = 0;
  469. if(!filename || !*filename || !strcmp(filename, "none") || !strcmp(filename, "/dev/null")) {
  470. filename = "/dev/null";
  471. devnull = 1;
  472. }
  473. if(!strcmp(filename, "syslog")) {
  474. filename = "/dev/null";
  475. devnull = 1;
  476. syslog_init();
  477. if(enabled_syslog) *enabled_syslog = 1;
  478. }
  479. else if(enabled_syslog) *enabled_syslog = 0;
  480. // don't do anything if the user is willing
  481. // to have the standard one
  482. if(!strcmp(filename, "system")) {
  483. if(fd != -1 && !is_stdaccess) {
  484. if(fd_ptr) *fd_ptr = fd;
  485. return fp;
  486. }
  487. filename = "stderr";
  488. }
  489. if(!strcmp(filename, "stdout"))
  490. f = STDOUT_FILENO;
  491. else if(!strcmp(filename, "stderr"))
  492. f = STDERR_FILENO;
  493. else {
  494. f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
  495. if(f == -1) {
  496. error("Cannot open file '%s'. Leaving %d to its default.", filename, fd);
  497. if(fd_ptr) *fd_ptr = fd;
  498. return fp;
  499. }
  500. }
  501. // if there is a level-2 file pointer
  502. // flush it before switching the level-1 fds
  503. if(fp)
  504. fflush(fp);
  505. if(devnull && is_stdaccess) {
  506. fd = -1;
  507. fp = NULL;
  508. }
  509. if(fd != f && fd != -1) {
  510. // it automatically closes
  511. int t = dup2(f, fd);
  512. if (t == -1) {
  513. error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
  514. close(f);
  515. if(fd_ptr) *fd_ptr = fd;
  516. return fp;
  517. }
  518. // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
  519. close(f);
  520. }
  521. else fd = f;
  522. if(!fp) {
  523. fp = fdopen(fd, "a");
  524. if (!fp)
  525. error("Cannot fdopen() fd %d ('%s')", fd, filename);
  526. else {
  527. if (setvbuf(fp, NULL, _IOLBF, 0) != 0)
  528. error("Cannot set line buffering on fd %d ('%s')", fd, filename);
  529. }
  530. }
  531. if(fd_ptr) *fd_ptr = fd;
  532. return fp;
  533. }
  534. void reopen_all_log_files() {
  535. if(stdout_filename)
  536. open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
  537. if(stdcollector_filename)
  538. open_log_file(STDERR_FILENO, stderr, stdcollector_filename, &collector_log_syslog, 0, NULL);
  539. if(stderr_filename) {
  540. log_lock();
  541. stderror = open_log_file(stdcollector_fd, stderror, stderr_filename, &error_log_syslog, 1, &stdcollector_fd);
  542. log_unlock();
  543. }
  544. #ifdef ENABLE_ACLK
  545. if (aclklog_enabled)
  546. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  547. #endif
  548. if(stdaccess_filename)
  549. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  550. if(stdhealth_filename)
  551. stdhealth = open_log_file(stdhealth_fd, stdhealth, stdhealth_filename, &health_log_syslog, 1, &stdhealth_fd);
  552. }
  553. void open_all_log_files() {
  554. // disable stdin
  555. open_log_file(STDIN_FILENO, stdin, "/dev/null", NULL, 0, NULL);
  556. open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
  557. open_log_file(STDERR_FILENO, stderr, stdcollector_filename, &collector_log_syslog, 0, NULL);
  558. log_lock();
  559. stderror = open_log_file(stdcollector_fd, NULL, stderr_filename, &error_log_syslog, 1, &stdcollector_fd);
  560. log_unlock();
  561. #ifdef ENABLE_ACLK
  562. if(aclklog_enabled)
  563. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  564. #endif
  565. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  566. stdhealth = open_log_file(stdhealth_fd, stdhealth, stdhealth_filename, &health_log_syslog, 1, &stdhealth_fd);
  567. }
  568. // ----------------------------------------------------------------------------
  569. // error log throttling
  570. time_t error_log_throttle_period = 1200;
  571. unsigned long error_log_errors_per_period = 200;
  572. unsigned long error_log_errors_per_period_backup = 0;
  573. int error_log_limit(int reset) {
  574. static time_t start = 0;
  575. static unsigned long counter = 0, prevented = 0;
  576. FILE *fp = (!stderror) ? stderr : stderror;
  577. // fprintf(fp, "FLOOD: counter=%lu, allowed=%lu, backup=%lu, period=%llu\n", counter, error_log_errors_per_period, error_log_errors_per_period_backup, (unsigned long long)error_log_throttle_period);
  578. // do not throttle if the period is 0
  579. if(error_log_throttle_period == 0)
  580. return 0;
  581. // prevent all logs if the errors per period is 0
  582. if(error_log_errors_per_period == 0)
  583. #ifdef NETDATA_INTERNAL_CHECKS
  584. return 0;
  585. #else
  586. return 1;
  587. #endif
  588. time_t now = now_monotonic_sec();
  589. if(!start) start = now;
  590. if(reset) {
  591. if(prevented) {
  592. char date[LOG_DATE_LENGTH];
  593. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  594. fprintf(
  595. fp,
  596. "%s: %s LOG FLOOD PROTECTION reset for process '%s' "
  597. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  598. date,
  599. program_name,
  600. program_name,
  601. prevented,
  602. (int64_t)(now - start));
  603. }
  604. start = now;
  605. counter = 0;
  606. prevented = 0;
  607. }
  608. // detect if we log too much
  609. counter++;
  610. if(now - start > error_log_throttle_period) {
  611. if(prevented) {
  612. char date[LOG_DATE_LENGTH];
  613. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  614. fprintf(
  615. fp,
  616. "%s: %s LOG FLOOD PROTECTION resuming logging from process '%s' "
  617. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  618. date,
  619. program_name,
  620. program_name,
  621. prevented,
  622. (int64_t)error_log_throttle_period);
  623. }
  624. // restart the period accounting
  625. start = now;
  626. counter = 1;
  627. prevented = 0;
  628. // log this error
  629. return 0;
  630. }
  631. if(counter > error_log_errors_per_period) {
  632. if(!prevented) {
  633. char date[LOG_DATE_LENGTH];
  634. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  635. fprintf(
  636. fp,
  637. "%s: %s LOG FLOOD PROTECTION too many logs (%lu logs in %"PRId64" seconds, threshold is set to %lu logs "
  638. "in %"PRId64" seconds). Preventing more logs from process '%s' for %"PRId64" seconds.\n",
  639. date,
  640. program_name,
  641. counter,
  642. (int64_t)(now - start),
  643. error_log_errors_per_period,
  644. (int64_t)error_log_throttle_period,
  645. program_name,
  646. (int64_t)(start + error_log_throttle_period - now));
  647. }
  648. prevented++;
  649. // prevent logging this error
  650. #ifdef NETDATA_INTERNAL_CHECKS
  651. return 0;
  652. #else
  653. return 1;
  654. #endif
  655. }
  656. return 0;
  657. }
  658. void error_log_limit_reset(void) {
  659. log_lock();
  660. error_log_errors_per_period = error_log_errors_per_period_backup;
  661. error_log_limit(1);
  662. log_unlock();
  663. }
  664. void error_log_limit_unlimited(void) {
  665. log_lock();
  666. error_log_errors_per_period = error_log_errors_per_period_backup;
  667. error_log_limit(1);
  668. error_log_errors_per_period = ((error_log_errors_per_period_backup * 10) < 10000) ? 10000 : (error_log_errors_per_period_backup * 10);
  669. log_unlock();
  670. }
  671. // ----------------------------------------------------------------------------
  672. // debug log
  673. void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  674. va_list args;
  675. char date[LOG_DATE_LENGTH];
  676. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  677. va_start( args, fmt );
  678. printf("%s: %s DEBUG : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
  679. vprintf(fmt, args);
  680. va_end( args );
  681. putchar('\n');
  682. if(output_log_syslog) {
  683. va_start( args, fmt );
  684. vsyslog(LOG_ERR, fmt, args );
  685. va_end( args );
  686. }
  687. fflush(stdout);
  688. }
  689. // ----------------------------------------------------------------------------
  690. // info log
  691. void info_int( int is_collector, const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt, ... )
  692. {
  693. va_list args;
  694. FILE *fp = (is_collector || !stderror) ? stderr : stderror;
  695. log_lock();
  696. // prevent logging too much
  697. if (error_log_limit(0)) {
  698. log_unlock();
  699. return;
  700. }
  701. if(collector_log_syslog) {
  702. va_start( args, fmt );
  703. vsyslog(LOG_INFO, fmt, args );
  704. va_end( args );
  705. }
  706. char date[LOG_DATE_LENGTH];
  707. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  708. va_start( args, fmt );
  709. #ifdef NETDATA_INTERNAL_CHECKS
  710. fprintf(fp, "%s: %s INFO : %s : (%04lu@%-20.20s:%-15.15s): ",
  711. date, program_name, netdata_thread_tag(), line, file, function);
  712. #else
  713. fprintf(fp, "%s: %s INFO : %s : ", date, program_name, netdata_thread_tag());
  714. #endif
  715. vfprintf(fp, fmt, args );
  716. va_end( args );
  717. fputc('\n', fp);
  718. log_unlock();
  719. }
  720. // ----------------------------------------------------------------------------
  721. // error log
  722. #if defined(STRERROR_R_CHAR_P)
  723. // GLIBC version of strerror_r
  724. static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
  725. #elif defined(HAVE_STRERROR_R)
  726. // POSIX version of strerror_r
  727. static const char *strerror_result(int a, const char *b) { (void)a; return b; }
  728. #elif defined(HAVE_C__GENERIC)
  729. // what a trick!
  730. // http://stackoverflow.com/questions/479207/function-overloading-in-c
  731. static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
  732. static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
  733. #define strerror_result(a, b) _Generic((a), \
  734. int: strerror_result_int, \
  735. char *: strerror_result_string \
  736. )(a, b)
  737. #else
  738. #error "cannot detect the format of function strerror_r()"
  739. #endif
  740. void error_limit_int(ERROR_LIMIT *erl, const char *prefix, const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt, ... ) {
  741. FILE *fp = (!stderror) ? stderr : stderror;
  742. if(erl->sleep_ut)
  743. sleep_usec(erl->sleep_ut);
  744. // save a copy of errno - just in case this function generates a new error
  745. int __errno = errno;
  746. va_list args;
  747. log_lock();
  748. erl->count++;
  749. time_t now = now_boottime_sec();
  750. if(now - erl->last_logged < erl->log_every) {
  751. log_unlock();
  752. return;
  753. }
  754. // prevent logging too much
  755. if (error_log_limit(0)) {
  756. log_unlock();
  757. return;
  758. }
  759. if(collector_log_syslog) {
  760. va_start( args, fmt );
  761. vsyslog(LOG_ERR, fmt, args );
  762. va_end( args );
  763. }
  764. char date[LOG_DATE_LENGTH];
  765. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  766. va_start( args, fmt );
  767. #ifdef NETDATA_INTERNAL_CHECKS
  768. fprintf(fp, "%s: %s %-5.5s : %s : (%04lu@%-20.20s:%-15.15s): ",
  769. date, program_name, prefix, netdata_thread_tag(), line, file, function);
  770. #else
  771. fprintf(fp, "%s: %s %-5.5s : %s : ", date, program_name, prefix, netdata_thread_tag());
  772. #endif
  773. vfprintf(fp, fmt, args );
  774. va_end( args );
  775. if(erl->count > 1)
  776. fprintf(fp, " (similar messages repeated %zu times in the last %llu secs)",
  777. erl->count, (unsigned long long)(erl->last_logged ? now - erl->last_logged : 0));
  778. if(erl->sleep_ut)
  779. fprintf(fp, " (sleeping for %llu microseconds every time this happens)", erl->sleep_ut);
  780. if(__errno) {
  781. char buf[1024];
  782. fprintf(fp,
  783. " (errno %d, %s)\n", __errno, strerror_result(strerror_r(__errno, buf, 1023), buf));
  784. errno = 0;
  785. }
  786. else
  787. fputc('\n', fp);
  788. erl->last_logged = now;
  789. erl->count = 0;
  790. log_unlock();
  791. }
  792. void error_int(int is_collector, const char *prefix, const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt, ... ) {
  793. // save a copy of errno - just in case this function generates a new error
  794. int __errno = errno;
  795. FILE *fp = (is_collector || !stderror) ? stderr : stderror;
  796. va_list args;
  797. log_lock();
  798. // prevent logging too much
  799. if (error_log_limit(0)) {
  800. log_unlock();
  801. return;
  802. }
  803. if(collector_log_syslog) {
  804. va_start( args, fmt );
  805. vsyslog(LOG_ERR, fmt, args );
  806. va_end( args );
  807. }
  808. char date[LOG_DATE_LENGTH];
  809. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  810. va_start( args, fmt );
  811. #ifdef NETDATA_INTERNAL_CHECKS
  812. fprintf(fp, "%s: %s %-5.5s : %s : (%04lu@%-20.20s:%-15.15s): ",
  813. date, program_name, prefix, netdata_thread_tag(), line, file, function);
  814. #else
  815. fprintf(fp, "%s: %s %-5.5s : %s : ", date, program_name, prefix, netdata_thread_tag());
  816. #endif
  817. vfprintf(fp, fmt, args );
  818. va_end( args );
  819. if(__errno) {
  820. char buf[1024];
  821. fprintf(fp,
  822. " (errno %d, %s)\n", __errno, strerror_result(strerror_r(__errno, buf, 1023), buf));
  823. errno = 0;
  824. }
  825. else
  826. fputc('\n', fp);
  827. log_unlock();
  828. }
  829. #ifdef NETDATA_INTERNAL_CHECKS
  830. static void crash_netdata(void) {
  831. // make Netdata core dump
  832. abort();
  833. }
  834. #endif
  835. #ifdef HAVE_BACKTRACE
  836. #define BT_BUF_SIZE 100
  837. static void print_call_stack(void) {
  838. FILE *fp = (!stderror) ? stderr : stderror;
  839. int nptrs;
  840. void *buffer[BT_BUF_SIZE];
  841. nptrs = backtrace(buffer, BT_BUF_SIZE);
  842. if(nptrs)
  843. backtrace_symbols_fd(buffer, nptrs, fileno(fp));
  844. }
  845. #endif
  846. void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  847. FILE *fp = (!stderror) ? stderr : stderror;
  848. // save a copy of errno - just in case this function generates a new error
  849. int __errno = errno;
  850. va_list args;
  851. const char *thread_tag;
  852. char os_threadname[NETDATA_THREAD_NAME_MAX + 1];
  853. if(collector_log_syslog) {
  854. va_start( args, fmt );
  855. vsyslog(LOG_CRIT, fmt, args );
  856. va_end( args );
  857. }
  858. thread_tag = netdata_thread_tag();
  859. if (!netdata_thread_tag_exists()) {
  860. os_thread_get_current_name_np(os_threadname);
  861. if ('\0' != os_threadname[0]) { /* If it is not an empty string replace "MAIN" thread_tag */
  862. thread_tag = os_threadname;
  863. }
  864. }
  865. char date[LOG_DATE_LENGTH];
  866. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  867. log_lock();
  868. va_start( args, fmt );
  869. #ifdef NETDATA_INTERNAL_CHECKS
  870. fprintf(fp,
  871. "%s: %s FATAL : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, thread_tag, line, file, function);
  872. #else
  873. fprintf(fp, "%s: %s FATAL : %s : ", date, program_name, thread_tag);
  874. #endif
  875. vfprintf(fp, fmt, args );
  876. va_end( args );
  877. perror(" # ");
  878. fputc('\n', fp);
  879. log_unlock();
  880. char action_data[70+1];
  881. snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, __errno);
  882. char action_result[60+1];
  883. const char *tag_to_send = thread_tag;
  884. // anonymize thread names
  885. if(strncmp(thread_tag, THREAD_TAG_STREAM_RECEIVER, strlen(THREAD_TAG_STREAM_RECEIVER)) == 0)
  886. tag_to_send = THREAD_TAG_STREAM_RECEIVER;
  887. if(strncmp(thread_tag, THREAD_TAG_STREAM_SENDER, strlen(THREAD_TAG_STREAM_SENDER)) == 0)
  888. tag_to_send = THREAD_TAG_STREAM_SENDER;
  889. snprintfz(action_result, 60, "%s:%s", program_name, tag_to_send);
  890. send_statistics("FATAL", action_result, action_data);
  891. #ifdef HAVE_BACKTRACE
  892. print_call_stack();
  893. #endif
  894. #ifdef NETDATA_INTERNAL_CHECKS
  895. crash_netdata();
  896. #endif
  897. netdata_cleanup_and_exit(1);
  898. }
  899. // ----------------------------------------------------------------------------
  900. // access log
  901. void log_access( const char *fmt, ... ) {
  902. va_list args;
  903. if(access_log_syslog) {
  904. va_start( args, fmt );
  905. vsyslog(LOG_INFO, fmt, args );
  906. va_end( args );
  907. }
  908. if(stdaccess) {
  909. static netdata_mutex_t access_mutex = NETDATA_MUTEX_INITIALIZER;
  910. if(web_server_is_multithreaded)
  911. netdata_mutex_lock(&access_mutex);
  912. char date[LOG_DATE_LENGTH];
  913. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  914. fprintf(stdaccess, "%s: ", date);
  915. va_start( args, fmt );
  916. vfprintf( stdaccess, fmt, args );
  917. va_end( args );
  918. fputc('\n', stdaccess);
  919. if(web_server_is_multithreaded)
  920. netdata_mutex_unlock(&access_mutex);
  921. }
  922. }
  923. // ----------------------------------------------------------------------------
  924. // health log
  925. void log_health( const char *fmt, ... ) {
  926. va_list args;
  927. if(health_log_syslog) {
  928. va_start( args, fmt );
  929. vsyslog(LOG_INFO, fmt, args );
  930. va_end( args );
  931. }
  932. if(stdhealth) {
  933. static netdata_mutex_t health_mutex = NETDATA_MUTEX_INITIALIZER;
  934. if(web_server_is_multithreaded)
  935. netdata_mutex_lock(&health_mutex);
  936. char date[LOG_DATE_LENGTH];
  937. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  938. fprintf(stdhealth, "%s: ", date);
  939. va_start( args, fmt );
  940. vfprintf( stdhealth, fmt, args );
  941. va_end( args );
  942. fputc('\n', stdhealth);
  943. if(web_server_is_multithreaded)
  944. netdata_mutex_unlock(&health_mutex);
  945. }
  946. }
  947. #ifdef ENABLE_ACLK
  948. void log_aclk_message_bin( const char *data, const size_t data_len, int tx, const char *mqtt_topic, const char *message_name) {
  949. if (aclklog) {
  950. static netdata_mutex_t aclklog_mutex = NETDATA_MUTEX_INITIALIZER;
  951. netdata_mutex_lock(&aclklog_mutex);
  952. char date[LOG_DATE_LENGTH];
  953. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  954. fprintf(aclklog, "%s: %s Msg:\"%s\", MQTT-topic:\"%s\": ", date, tx ? "OUTGOING" : "INCOMING", message_name, mqtt_topic);
  955. fwrite(data, data_len, 1, aclklog);
  956. fputc('\n', aclklog);
  957. netdata_mutex_unlock(&aclklog_mutex);
  958. }
  959. }
  960. #endif