log.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. netdata_log_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. netdata_log_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. // netdata_log_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. netdata_log_error("Cannot fdopen() fd %d ('%s')", fd, filename);
  526. else {
  527. if (setvbuf(fp, NULL, _IOLBF, 0) != 0)
  528. netdata_log_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. // Netdata starts using stderr and if it has success to open file it redirects
  541. FILE *fp = open_log_file(stdcollector_fd, stderror, stderr_filename,
  542. &error_log_syslog, 1, &stdcollector_fd);
  543. if (fp)
  544. stderror = fp;
  545. }
  546. #ifdef ENABLE_ACLK
  547. if (aclklog_enabled)
  548. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  549. #endif
  550. if(stdaccess_filename)
  551. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  552. if(stdhealth_filename)
  553. stdhealth = open_log_file(stdhealth_fd, stdhealth, stdhealth_filename, &health_log_syslog, 1, &stdhealth_fd);
  554. }
  555. void open_all_log_files() {
  556. // disable stdin
  557. open_log_file(STDIN_FILENO, stdin, "/dev/null", NULL, 0, NULL);
  558. open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
  559. open_log_file(STDERR_FILENO, stderr, stdcollector_filename, &collector_log_syslog, 0, NULL);
  560. // Netdata starts using stderr and if it has success to open file it redirects
  561. FILE *fp = open_log_file(stdcollector_fd, NULL, stderr_filename, &error_log_syslog, 1, &stdcollector_fd);
  562. if (fp)
  563. stderror = fp;
  564. #ifdef ENABLE_ACLK
  565. if(aclklog_enabled)
  566. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  567. #endif
  568. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  569. stdhealth = open_log_file(stdhealth_fd, stdhealth, stdhealth_filename, &health_log_syslog, 1, &stdhealth_fd);
  570. }
  571. // ----------------------------------------------------------------------------
  572. // error log throttling
  573. time_t error_log_throttle_period = 1200;
  574. unsigned long error_log_errors_per_period = 200;
  575. unsigned long error_log_errors_per_period_backup = 0;
  576. int error_log_limit(int reset) {
  577. static time_t start = 0;
  578. static unsigned long counter = 0, prevented = 0;
  579. FILE *fp = stderror ? stderror : stderr;
  580. // 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);
  581. // do not throttle if the period is 0
  582. if(error_log_throttle_period == 0)
  583. return 0;
  584. // prevent all logs if the errors per period is 0
  585. if(error_log_errors_per_period == 0)
  586. #ifdef NETDATA_INTERNAL_CHECKS
  587. return 0;
  588. #else
  589. return 1;
  590. #endif
  591. time_t now = now_monotonic_sec();
  592. if(!start) start = now;
  593. if(reset) {
  594. if(prevented) {
  595. char date[LOG_DATE_LENGTH];
  596. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  597. fprintf(
  598. fp,
  599. "%s: %s LOG FLOOD PROTECTION reset for process '%s' "
  600. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  601. date,
  602. program_name,
  603. program_name,
  604. prevented,
  605. (int64_t)(now - start));
  606. }
  607. start = now;
  608. counter = 0;
  609. prevented = 0;
  610. }
  611. // detect if we log too much
  612. counter++;
  613. if(now - start > error_log_throttle_period) {
  614. if(prevented) {
  615. char date[LOG_DATE_LENGTH];
  616. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  617. fprintf(
  618. fp,
  619. "%s: %s LOG FLOOD PROTECTION resuming logging from process '%s' "
  620. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  621. date,
  622. program_name,
  623. program_name,
  624. prevented,
  625. (int64_t)error_log_throttle_period);
  626. }
  627. // restart the period accounting
  628. start = now;
  629. counter = 1;
  630. prevented = 0;
  631. // log this error
  632. return 0;
  633. }
  634. if(counter > error_log_errors_per_period) {
  635. if(!prevented) {
  636. char date[LOG_DATE_LENGTH];
  637. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  638. fprintf(
  639. fp,
  640. "%s: %s LOG FLOOD PROTECTION too many logs (%lu logs in %"PRId64" seconds, threshold is set to %lu logs "
  641. "in %"PRId64" seconds). Preventing more logs from process '%s' for %"PRId64" seconds.\n",
  642. date,
  643. program_name,
  644. counter,
  645. (int64_t)(now - start),
  646. error_log_errors_per_period,
  647. (int64_t)error_log_throttle_period,
  648. program_name,
  649. (int64_t)(start + error_log_throttle_period - now));
  650. }
  651. prevented++;
  652. // prevent logging this error
  653. #ifdef NETDATA_INTERNAL_CHECKS
  654. return 0;
  655. #else
  656. return 1;
  657. #endif
  658. }
  659. return 0;
  660. }
  661. void error_log_limit_reset(void) {
  662. log_lock();
  663. error_log_errors_per_period = error_log_errors_per_period_backup;
  664. error_log_limit(1);
  665. log_unlock();
  666. }
  667. void error_log_limit_unlimited(void) {
  668. log_lock();
  669. error_log_errors_per_period = error_log_errors_per_period_backup;
  670. error_log_limit(1);
  671. error_log_errors_per_period = ((error_log_errors_per_period_backup * 10) < 10000) ? 10000 : (error_log_errors_per_period_backup * 10);
  672. log_unlock();
  673. }
  674. // ----------------------------------------------------------------------------
  675. // debug log
  676. void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  677. va_list args;
  678. char date[LOG_DATE_LENGTH];
  679. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  680. va_start( args, fmt );
  681. printf("%s: %s DEBUG : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
  682. vprintf(fmt, args);
  683. va_end( args );
  684. putchar('\n');
  685. if(output_log_syslog) {
  686. va_start( args, fmt );
  687. vsyslog(LOG_ERR, fmt, args );
  688. va_end( args );
  689. }
  690. fflush(stdout);
  691. }
  692. // ----------------------------------------------------------------------------
  693. // info log
  694. 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, ... )
  695. {
  696. va_list args;
  697. FILE *fp = (is_collector || !stderror) ? stderr : stderror;
  698. log_lock();
  699. // prevent logging too much
  700. if (error_log_limit(0)) {
  701. log_unlock();
  702. return;
  703. }
  704. if(collector_log_syslog) {
  705. va_start( args, fmt );
  706. vsyslog(LOG_INFO, fmt, args );
  707. va_end( args );
  708. }
  709. char date[LOG_DATE_LENGTH];
  710. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  711. va_start( args, fmt );
  712. #ifdef NETDATA_INTERNAL_CHECKS
  713. fprintf(fp, "%s: %s INFO : %s : (%04lu@%-20.20s:%-15.15s): ",
  714. date, program_name, netdata_thread_tag(), line, file, function);
  715. #else
  716. fprintf(fp, "%s: %s INFO : %s : ", date, program_name, netdata_thread_tag());
  717. #endif
  718. vfprintf(fp, fmt, args );
  719. va_end( args );
  720. fputc('\n', fp);
  721. log_unlock();
  722. }
  723. // ----------------------------------------------------------------------------
  724. // error log
  725. #if defined(STRERROR_R_CHAR_P)
  726. // GLIBC version of strerror_r
  727. static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
  728. #elif defined(HAVE_STRERROR_R)
  729. // POSIX version of strerror_r
  730. static const char *strerror_result(int a, const char *b) { (void)a; return b; }
  731. #elif defined(HAVE_C__GENERIC)
  732. // what a trick!
  733. // http://stackoverflow.com/questions/479207/function-overloading-in-c
  734. static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
  735. static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
  736. #define strerror_result(a, b) _Generic((a), \
  737. int: strerror_result_int, \
  738. char *: strerror_result_string \
  739. )(a, b)
  740. #else
  741. #error "cannot detect the format of function strerror_r()"
  742. #endif
  743. 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, ... ) {
  744. FILE *fp = stderror ? stderror : stderr;
  745. if(erl->sleep_ut)
  746. sleep_usec(erl->sleep_ut);
  747. // save a copy of errno - just in case this function generates a new error
  748. int __errno = errno;
  749. va_list args;
  750. log_lock();
  751. erl->count++;
  752. time_t now = now_boottime_sec();
  753. if(now - erl->last_logged < erl->log_every) {
  754. log_unlock();
  755. return;
  756. }
  757. // prevent logging too much
  758. if (error_log_limit(0)) {
  759. log_unlock();
  760. return;
  761. }
  762. if(collector_log_syslog) {
  763. va_start( args, fmt );
  764. vsyslog(LOG_ERR, fmt, args );
  765. va_end( args );
  766. }
  767. char date[LOG_DATE_LENGTH];
  768. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  769. va_start( args, fmt );
  770. #ifdef NETDATA_INTERNAL_CHECKS
  771. fprintf(fp, "%s: %s %-5.5s : %s : (%04lu@%-20.20s:%-15.15s): ",
  772. date, program_name, prefix, netdata_thread_tag(), line, file, function);
  773. #else
  774. fprintf(fp, "%s: %s %-5.5s : %s : ", date, program_name, prefix, netdata_thread_tag());
  775. #endif
  776. vfprintf(fp, fmt, args );
  777. va_end( args );
  778. if(erl->count > 1)
  779. fprintf(fp, " (similar messages repeated %zu times in the last %llu secs)",
  780. erl->count, (unsigned long long)(erl->last_logged ? now - erl->last_logged : 0));
  781. if(erl->sleep_ut)
  782. fprintf(fp, " (sleeping for %llu microseconds every time this happens)", erl->sleep_ut);
  783. if(__errno) {
  784. char buf[1024];
  785. fprintf(fp,
  786. " (errno %d, %s)\n", __errno, strerror_result(strerror_r(__errno, buf, 1023), buf));
  787. errno = 0;
  788. }
  789. else
  790. fputc('\n', fp);
  791. erl->last_logged = now;
  792. erl->count = 0;
  793. log_unlock();
  794. }
  795. 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, ... ) {
  796. // save a copy of errno - just in case this function generates a new error
  797. int __errno = errno;
  798. FILE *fp = (is_collector || !stderror) ? stderr : stderror;
  799. va_list args;
  800. log_lock();
  801. // prevent logging too much
  802. if (error_log_limit(0)) {
  803. log_unlock();
  804. return;
  805. }
  806. if(collector_log_syslog) {
  807. va_start( args, fmt );
  808. vsyslog(LOG_ERR, fmt, args );
  809. va_end( args );
  810. }
  811. char date[LOG_DATE_LENGTH];
  812. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  813. va_start( args, fmt );
  814. #ifdef NETDATA_INTERNAL_CHECKS
  815. fprintf(fp, "%s: %s %-5.5s : %s : (%04lu@%-20.20s:%-15.15s): ",
  816. date, program_name, prefix, netdata_thread_tag(), line, file, function);
  817. #else
  818. fprintf(fp, "%s: %s %-5.5s : %s : ", date, program_name, prefix, netdata_thread_tag());
  819. #endif
  820. vfprintf(fp, fmt, args );
  821. va_end( args );
  822. if(__errno) {
  823. char buf[1024];
  824. fprintf(fp,
  825. " (errno %d, %s)\n", __errno, strerror_result(strerror_r(__errno, buf, 1023), buf));
  826. errno = 0;
  827. }
  828. else
  829. fputc('\n', fp);
  830. log_unlock();
  831. }
  832. #ifdef NETDATA_INTERNAL_CHECKS
  833. static void crash_netdata(void) {
  834. // make Netdata core dump
  835. abort();
  836. }
  837. #endif
  838. #ifdef HAVE_BACKTRACE
  839. #define BT_BUF_SIZE 100
  840. static void print_call_stack(void) {
  841. FILE *fp = (!stderror) ? stderr : stderror;
  842. int nptrs;
  843. void *buffer[BT_BUF_SIZE];
  844. nptrs = backtrace(buffer, BT_BUF_SIZE);
  845. if(nptrs)
  846. backtrace_symbols_fd(buffer, nptrs, fileno(fp));
  847. }
  848. #endif
  849. void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  850. FILE *fp = stderror ? stderror : stderr;
  851. // save a copy of errno - just in case this function generates a new error
  852. int __errno = errno;
  853. va_list args;
  854. const char *thread_tag;
  855. char os_threadname[NETDATA_THREAD_NAME_MAX + 1];
  856. if(collector_log_syslog) {
  857. va_start( args, fmt );
  858. vsyslog(LOG_CRIT, fmt, args );
  859. va_end( args );
  860. }
  861. thread_tag = netdata_thread_tag();
  862. if (!netdata_thread_tag_exists()) {
  863. os_thread_get_current_name_np(os_threadname);
  864. if ('\0' != os_threadname[0]) { /* If it is not an empty string replace "MAIN" thread_tag */
  865. thread_tag = os_threadname;
  866. }
  867. }
  868. char date[LOG_DATE_LENGTH];
  869. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  870. log_lock();
  871. va_start( args, fmt );
  872. #ifdef NETDATA_INTERNAL_CHECKS
  873. fprintf(fp,
  874. "%s: %s FATAL : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, thread_tag, line, file, function);
  875. #else
  876. fprintf(fp, "%s: %s FATAL : %s : ", date, program_name, thread_tag);
  877. #endif
  878. vfprintf(fp, fmt, args );
  879. va_end( args );
  880. perror(" # ");
  881. fputc('\n', fp);
  882. log_unlock();
  883. char action_data[70+1];
  884. snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, __errno);
  885. char action_result[60+1];
  886. const char *tag_to_send = thread_tag;
  887. // anonymize thread names
  888. if(strncmp(thread_tag, THREAD_TAG_STREAM_RECEIVER, strlen(THREAD_TAG_STREAM_RECEIVER)) == 0)
  889. tag_to_send = THREAD_TAG_STREAM_RECEIVER;
  890. if(strncmp(thread_tag, THREAD_TAG_STREAM_SENDER, strlen(THREAD_TAG_STREAM_SENDER)) == 0)
  891. tag_to_send = THREAD_TAG_STREAM_SENDER;
  892. snprintfz(action_result, 60, "%s:%s", program_name, tag_to_send);
  893. send_statistics("FATAL", action_result, action_data);
  894. #ifdef HAVE_BACKTRACE
  895. print_call_stack();
  896. #endif
  897. #ifdef NETDATA_INTERNAL_CHECKS
  898. crash_netdata();
  899. #endif
  900. netdata_cleanup_and_exit(1);
  901. }
  902. // ----------------------------------------------------------------------------
  903. // access log
  904. void netdata_log_access( const char *fmt, ... ) {
  905. va_list args;
  906. if(access_log_syslog) {
  907. va_start( args, fmt );
  908. vsyslog(LOG_INFO, fmt, args );
  909. va_end( args );
  910. }
  911. if(stdaccess) {
  912. static netdata_mutex_t access_mutex = NETDATA_MUTEX_INITIALIZER;
  913. if(web_server_is_multithreaded)
  914. netdata_mutex_lock(&access_mutex);
  915. char date[LOG_DATE_LENGTH];
  916. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  917. fprintf(stdaccess, "%s: ", date);
  918. va_start( args, fmt );
  919. vfprintf( stdaccess, fmt, args );
  920. va_end( args );
  921. fputc('\n', stdaccess);
  922. if(web_server_is_multithreaded)
  923. netdata_mutex_unlock(&access_mutex);
  924. }
  925. }
  926. // ----------------------------------------------------------------------------
  927. // health log
  928. void netdata_log_health( const char *fmt, ... ) {
  929. va_list args;
  930. if(health_log_syslog) {
  931. va_start( args, fmt );
  932. vsyslog(LOG_INFO, fmt, args );
  933. va_end( args );
  934. }
  935. if(stdhealth) {
  936. static netdata_mutex_t health_mutex = NETDATA_MUTEX_INITIALIZER;
  937. if(web_server_is_multithreaded)
  938. netdata_mutex_lock(&health_mutex);
  939. char date[LOG_DATE_LENGTH];
  940. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  941. fprintf(stdhealth, "%s: ", date);
  942. va_start( args, fmt );
  943. vfprintf( stdhealth, fmt, args );
  944. va_end( args );
  945. fputc('\n', stdhealth);
  946. if(web_server_is_multithreaded)
  947. netdata_mutex_unlock(&health_mutex);
  948. }
  949. }
  950. #ifdef ENABLE_ACLK
  951. void log_aclk_message_bin( const char *data, const size_t data_len, int tx, const char *mqtt_topic, const char *message_name) {
  952. if (aclklog) {
  953. static netdata_mutex_t aclklog_mutex = NETDATA_MUTEX_INITIALIZER;
  954. netdata_mutex_lock(&aclklog_mutex);
  955. char date[LOG_DATE_LENGTH];
  956. log_date(date, LOG_DATE_LENGTH, now_realtime_sec());
  957. fprintf(aclklog, "%s: %s Msg:\"%s\", MQTT-topic:\"%s\": ", date, tx ? "OUTGOING" : "INCOMING", message_name, mqtt_topic);
  958. fwrite(data, data_len, 1, aclklog);
  959. fputc('\n', aclklog);
  960. netdata_mutex_unlock(&aclklog_mutex);
  961. }
  962. }
  963. #endif