log2journal-help.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "log2journal.h"
  3. static void config_dir_print_available(void) {
  4. const char *path = LOG2JOURNAL_CONFIG_PATH;
  5. DIR *dir;
  6. struct dirent *entry;
  7. dir = opendir(path);
  8. if (dir == NULL) {
  9. log2stderr(" >>> Cannot open directory '%s'", path);
  10. return;
  11. }
  12. size_t column_width = 80;
  13. size_t current_columns = 0;
  14. while ((entry = readdir(dir))) {
  15. if (entry->d_type == DT_REG) { // Check if it's a regular file
  16. const char *file_name = entry->d_name;
  17. size_t len = strlen(file_name);
  18. if (len >= 5 && strcmp(file_name + len - 5, ".yaml") == 0) {
  19. // Remove the ".yaml" extension
  20. len -= 5;
  21. if (current_columns + len + 1 > column_width) {
  22. // Start a new line if the current line is full
  23. printf("\n ");
  24. current_columns = 0;
  25. }
  26. printf("%.*s ", (int)len, file_name); // Print the filename without extension
  27. current_columns += len + 1; // Add filename length and a space
  28. }
  29. }
  30. }
  31. closedir(dir);
  32. printf("\n"); // Add a newline at the end
  33. }
  34. void log2journal_command_line_help(const char *name) {
  35. printf("\n");
  36. printf("Netdata log2journal " PACKAGE_VERSION "\n");
  37. printf("\n");
  38. printf("Convert logs to systemd Journal Export Format.\n");
  39. printf("\n");
  40. printf(" - JSON logs: extracts all JSON fields.\n");
  41. printf(" - logfmt logs: extracts all logfmt fields.\n");
  42. printf(" - free-form logs: uses PCRE2 patterns to extracts fields.\n");
  43. printf("\n");
  44. printf("Usage: %s [OPTIONS] PATTERN|json\n", name);
  45. printf("\n");
  46. printf("Options:\n");
  47. printf("\n");
  48. #ifdef HAVE_LIBYAML
  49. printf(" --file /path/to/file.yaml\n");
  50. printf(" Read yaml configuration file for instructions.\n");
  51. printf("\n");
  52. printf(" --config CONFIG_NAME\n");
  53. printf(" Run with the internal configuration named CONFIG_NAME\n");
  54. printf(" Available internal configs:\n");
  55. printf("\n");
  56. config_dir_print_available();
  57. printf("\n");
  58. #else
  59. printf(" IMPORTANT:\n");
  60. printf(" YAML configuration parsing is not compiled in this binary.\n");
  61. printf("\n");
  62. #endif
  63. printf(" --show-config\n");
  64. printf(" Show the configuration in YAML format before starting the job.\n");
  65. printf(" This is also an easy way to convert command line parameters to yaml.\n");
  66. printf("\n");
  67. printf(" --filename-key KEY\n");
  68. printf(" Add a field with KEY as the key and the current filename as value.\n");
  69. printf(" Automatically detects filenames when piped after 'tail -F',\n");
  70. printf(" and tail matches multiple filenames.\n");
  71. printf(" To inject the filename when tailing a single file, use --inject.\n");
  72. printf("\n");
  73. printf(" --unmatched-key KEY\n");
  74. printf(" Include unmatched log entries in the output with KEY as the field name.\n");
  75. printf(" Use this to include unmatched entries to the output stream.\n");
  76. printf(" Usually it should be set to --unmatched-key=MESSAGE so that the\n");
  77. printf(" unmatched entry will appear as the log message in the journals.\n");
  78. printf(" Use --inject-unmatched to inject additional fields to unmatched lines.\n");
  79. printf("\n");
  80. printf(" --duplicate TARGET=KEY1[,KEY2[,KEY3[,...]]\n");
  81. printf(" Create a new key called TARGET, duplicating the values of the keys\n");
  82. printf(" given. Useful for further processing. When multiple keys are given,\n");
  83. printf(" their values are separated by comma.\n");
  84. printf(" Up to %d duplications can be given on the command line, and up to\n", MAX_KEY_DUPS);
  85. printf(" %d keys per duplication command are allowed.\n", MAX_KEY_DUPS_KEYS);
  86. printf("\n");
  87. printf(" --inject LINE\n");
  88. printf(" Inject constant fields to the output (both matched and unmatched logs).\n");
  89. printf(" --inject entries are added to unmatched lines too, when their key is\n");
  90. printf(" not used in --inject-unmatched (--inject-unmatched override --inject).\n");
  91. printf(" Up to %d fields can be injected.\n", MAX_INJECTIONS);
  92. printf("\n");
  93. printf(" --inject-unmatched LINE\n");
  94. printf(" Inject lines into the output for each unmatched log entry.\n");
  95. printf(" Usually, --inject-unmatched=PRIORITY=3 is needed to mark the unmatched\n");
  96. printf(" lines as errors, so that they can easily be spotted in the journals.\n");
  97. printf(" Up to %d such lines can be injected.\n", MAX_INJECTIONS);
  98. printf("\n");
  99. printf(" --rewrite KEY=/SearchPattern/ReplacePattern\n");
  100. printf(" Apply a rewrite rule to the values of a specific key.\n");
  101. printf(" The first character after KEY= is the separator, which should also\n");
  102. printf(" be used between the search pattern and the replacement pattern.\n");
  103. printf(" The search pattern is a PCRE2 regular expression, and the replacement\n");
  104. printf(" pattern supports literals and named capture groups from the search pattern.\n");
  105. printf(" Example:\n");
  106. printf(" --rewrite DATE=/^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$/\n");
  107. printf(" ${day}/${month}/${year}\n");
  108. printf(" This will rewrite dates in the format YYYY-MM-DD to DD/MM/YYYY.\n");
  109. printf("\n");
  110. printf(" Only one rewrite rule is applied per key; the sequence of rewrites stops\n");
  111. printf(" for the key once a rule matches it. This allows providing a sequence of\n");
  112. printf(" independent rewriting rules for the same key, matching the different values\n");
  113. printf(" the key may get, and also provide a catch-all rewrite rule at the end of the\n");
  114. printf(" sequence for setting the key value if no other rule matched it.\n");
  115. printf("\n");
  116. printf(" The combination of duplicating keys with the values of multiple other keys\n");
  117. printf(" combined with multiple rewrite rules, allows creating complex rules for\n");
  118. printf(" rewriting key values.\n");
  119. printf(" Up to %d rewriting rules are allowed.\n", MAX_REWRITES);
  120. printf("\n");
  121. printf(" --prefix PREFIX\n");
  122. printf(" Prefix all JSON or logfmt fields with PREFIX.\n");
  123. printf("\n");
  124. printf(" --rename NEW=OLD\n");
  125. printf(" Rename fields, before rewriting their values.\n");
  126. printf(" Up to %d renaming rules are allowed.\n", MAX_RENAMES);
  127. printf("\n");
  128. printf(" -h, --help\n");
  129. printf(" Display this help and exit.\n");
  130. printf("\n");
  131. printf(" PATTERN\n");
  132. printf(" PATTERN should be a valid PCRE2 regular expression.\n");
  133. printf(" RE2 regular expressions (like the ones usually used in Go applications),\n");
  134. printf(" are usually valid PCRE2 patterns too.\n");
  135. printf(" Regular expressions without named groups are evaluated but their matches\n");
  136. printf(" are not added to the output.\n");
  137. printf("\n");
  138. printf(" JSON mode\n");
  139. printf(" JSON mode is enabled when the pattern is set to: json\n");
  140. printf(" Field names are extracted from the JSON logs and are converted to the\n");
  141. printf(" format expected by Journal Export Format (all caps, only _ is allowed).\n");
  142. printf(" Prefixing is enabled in this mode.\n");
  143. printf(" logfmt mode\n");
  144. printf(" logfmt mode is enabled when the pattern is set to: logfmt\n");
  145. printf(" Field names are extracted from the logfmt logs and are converted to the\n");
  146. printf(" format expected by Journal Export Format (all caps, only _ is allowed).\n");
  147. printf(" Prefixing is enabled in this mode.\n");
  148. printf("\n");
  149. printf("\n");
  150. printf("The program accepts all parameters as both --option=value and --option value.\n");
  151. printf("\n");
  152. printf("The maximum line length accepted is %d characters.\n", MAX_LINE_LENGTH);
  153. printf("The maximum number of fields in the PCRE2 pattern is %d.\n", OVECCOUNT / 3);
  154. printf("\n");
  155. printf("PIPELINE AND SEQUENCE OF PROCESSING\n");
  156. printf("\n");
  157. printf("This is a simple diagram of the pipeline taking place:\n");
  158. printf("\n");
  159. printf(" +---------------------------------------------------+\n");
  160. printf(" | INPUT |\n");
  161. printf(" +---------------------------------------------------+\n");
  162. printf(" v v\n");
  163. printf(" +---------------------------------+ |\n");
  164. printf(" | EXTRACT FIELDS AND VALUES | |\n");
  165. printf(" +---------------------------------+ |\n");
  166. printf(" v v |\n");
  167. printf(" +---------------+ +--------------+ |\n");
  168. printf(" | DUPLICATE | | RENAME | |\n");
  169. printf(" | create fields | | change the | |\n");
  170. printf(" | with values | | field name | |\n");
  171. printf(" +---------------+ +--------------+ |\n");
  172. printf(" v v v\n");
  173. printf(" +---------------------------------+ +--------------+\n");
  174. printf(" | REWRITE PIPELINES | | INJECT |\n");
  175. printf(" | altering keys and values | | constants |\n");
  176. printf(" +---------------------------------+ +--------------+\n");
  177. printf(" v v\n");
  178. printf(" +---------------------------------------------------+\n");
  179. printf(" | OUTPUT |\n");
  180. printf(" +---------------------------------------------------+\n");
  181. printf("\n");
  182. printf("JOURNAL FIELDS RULES (enforced by systemd-journald)\n");
  183. printf("\n");
  184. printf(" - field names can be up to 64 characters\n");
  185. printf(" - the only allowed field characters are A-Z, 0-9 and underscore\n");
  186. printf(" - the first character of fields cannot be a digit\n");
  187. printf(" - protected journal fields start with underscore:\n");
  188. printf(" * they are accepted by systemd-journal-remote\n");
  189. printf(" * they are NOT accepted by a local systemd-journald\n");
  190. printf("\n");
  191. printf(" For best results, always include these fields:\n");
  192. printf("\n");
  193. printf(" MESSAGE=TEXT\n");
  194. printf(" The MESSAGE is the body of the log entry.\n");
  195. printf(" This field is what we usually see in our logs.\n");
  196. printf("\n");
  197. printf(" PRIORITY=NUMBER\n");
  198. printf(" PRIORITY sets the severity of the log entry.\n");
  199. printf(" 0=emerg, 1=alert, 2=crit, 3=err, 4=warn, 5=notice, 6=info, 7=debug\n");
  200. printf(" - Emergency events (0) are usually broadcast to all terminals.\n");
  201. printf(" - Emergency, alert, critical, and error (0-3) are usually colored red.\n");
  202. printf(" - Warning (4) entries are usually colored yellow.\n");
  203. printf(" - Notice (5) entries are usually bold or have a brighter white color.\n");
  204. printf(" - Info (6) entries are the default.\n");
  205. printf(" - Debug (7) entries are usually grayed or dimmed.\n");
  206. printf("\n");
  207. printf(" SYSLOG_IDENTIFIER=NAME\n");
  208. printf(" SYSLOG_IDENTIFIER sets the name of application.\n");
  209. printf(" Use something descriptive, like: SYSLOG_IDENTIFIER=nginx-logs\n");
  210. printf("\n");
  211. printf("You can find the most common fields at 'man systemd.journal-fields'.\n");
  212. printf("\n");
  213. }