log.c 30 KB

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