categories.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. [role="xpack"]
  2. [[ml-configuring-categories]]
  3. === Categorizing log messages
  4. Application log events are often unstructured and contain variable data. For
  5. example:
  6. //Obtained from it_ops_new_app_logs.json
  7. [source,js]
  8. ----------------------------------
  9. {"time":1454516381000,"message":"org.jdbi.v2.exceptions.UnableToExecuteStatementException: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request [statement:\"SELECT id, customer_id, name, force_disabled, enabled FROM customers\"]","type":"logs"}
  10. ----------------------------------
  11. //NOTCONSOLE
  12. You can use {ml} to observe the static parts of the message, cluster similar
  13. messages together, and classify them into message categories.
  14. The {ml} model learns what volume and pattern is normal for each category over
  15. time. You can then detect anomalies and surface rare events or unusual types of
  16. messages by using count or rare functions. For example:
  17. //Obtained from it_ops_new_app_logs.sh
  18. [source,console]
  19. ----------------------------------
  20. PUT _ml/anomaly_detectors/it_ops_new_logs
  21. {
  22. "description" : "IT Ops Application Logs",
  23. "analysis_config" : {
  24. "categorization_field_name": "message", <1>
  25. "bucket_span":"30m",
  26. "detectors" :[{
  27. "function":"count",
  28. "by_field_name": "mlcategory", <2>
  29. "detector_description": "Unusual message counts"
  30. }],
  31. "categorization_filters":[ "\\[statement:.*\\]"]
  32. },
  33. "analysis_limits":{
  34. "categorization_examples_limit": 5
  35. },
  36. "data_description" : {
  37. "time_field":"time",
  38. "time_format": "epoch_ms"
  39. }
  40. }
  41. ----------------------------------
  42. // TEST[skip:needs-licence]
  43. <1> The `categorization_field_name` property indicates which field will be
  44. categorized.
  45. <2> The resulting categories are used in a detector by setting `by_field_name`,
  46. `over_field_name`, or `partition_field_name` to the keyword `mlcategory`. If you
  47. do not specify this keyword in one of those properties, the API request fails.
  48. The optional `categorization_examples_limit` property specifies the
  49. maximum number of examples that are stored in memory and in the results data
  50. store for each category. The default value is `4`. Note that this setting does
  51. not affect the categorization; it just affects the list of visible examples. If
  52. you increase this value, more examples are available, but you must have more
  53. storage available. If you set this value to `0`, no examples are stored.
  54. The optional `categorization_filters` property can contain an array of regular
  55. expressions. If a categorization field value matches the regular expression, the
  56. portion of the field that is matched is not taken into consideration when
  57. defining categories. The categorization filters are applied in the order they
  58. are listed in the job configuration, which allows you to disregard multiple
  59. sections of the categorization field value. In this example, we have decided that
  60. we do not want the detailed SQL to be considered in the message categorization.
  61. This particular categorization filter removes the SQL statement from the categorization
  62. algorithm.
  63. If your data is stored in {es}, you can create an advanced {anomaly-job} with
  64. these same properties:
  65. [role="screenshot"]
  66. image::images/ml-category-advanced.jpg["Advanced job configuration options related to categorization"]
  67. NOTE: To add the `categorization_examples_limit` property, you must use the
  68. **Edit JSON** tab and copy the `analysis_limits` object from the API example.
  69. [float]
  70. [[ml-configuring-analyzer]]
  71. ==== Customizing the categorization analyzer
  72. Categorization uses English dictionary words to identify log message categories.
  73. By default, it also uses English tokenization rules. For this reason, if you use
  74. the default categorization analyzer, only English language log messages are
  75. supported, as described in the <<ml-limitations>>.
  76. You can, however, change the tokenization rules by customizing the way the
  77. categorization field values are interpreted. For example:
  78. [source,console]
  79. ----------------------------------
  80. PUT _ml/anomaly_detectors/it_ops_new_logs2
  81. {
  82. "description" : "IT Ops Application Logs",
  83. "analysis_config" : {
  84. "categorization_field_name": "message",
  85. "bucket_span":"30m",
  86. "detectors" :[{
  87. "function":"count",
  88. "by_field_name": "mlcategory",
  89. "detector_description": "Unusual message counts"
  90. }],
  91. "categorization_analyzer":{
  92. "char_filter": [
  93. { "type": "pattern_replace", "pattern": "\\[statement:.*\\]" } <1>
  94. ],
  95. "tokenizer": "ml_classic", <2>
  96. "filter": [
  97. { "type" : "stop", "stopwords": [
  98. "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday",
  99. "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  100. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
  101. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  102. "GMT", "UTC"
  103. ] } <3>
  104. ]
  105. }
  106. },
  107. "analysis_limits":{
  108. "categorization_examples_limit": 5
  109. },
  110. "data_description" : {
  111. "time_field":"time",
  112. "time_format": "epoch_ms"
  113. }
  114. }
  115. ----------------------------------
  116. // TEST[skip:needs-licence]
  117. <1> The
  118. {ref}/analysis-pattern-replace-charfilter.html[`pattern_replace` character filter]
  119. here achieves exactly the same as the `categorization_filters` in the first
  120. example.
  121. <2> The `ml_classic` tokenizer works like the non-customizable tokenization
  122. that was used for categorization in older versions of machine learning. If you
  123. want the same categorization behavior as older versions, use this property value.
  124. <3> By default, English day or month words are filtered from log messages before
  125. categorization. If your logs are in a different language and contain
  126. dates, you might get better results by filtering the day or month words in your
  127. language.
  128. The optional `categorization_analyzer` property allows even greater customization
  129. of how categorization interprets the categorization field value. It can refer to
  130. a built-in {es} analyzer or a combination of zero or more character filters,
  131. a tokenizer, and zero or more token filters. If you omit the
  132. `categorization_analyzer`, the following default values are used:
  133. [source,console]
  134. --------------------------------------------------
  135. POST _ml/anomaly_detectors/_validate
  136. {
  137. "analysis_config" : {
  138. "categorization_analyzer" : {
  139. "tokenizer" : "ml_classic",
  140. "filter" : [
  141. { "type" : "stop", "stopwords": [
  142. "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday",
  143. "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  144. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
  145. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  146. "GMT", "UTC"
  147. ] }
  148. ]
  149. },
  150. "categorization_field_name": "message",
  151. "detectors" :[{
  152. "function":"count",
  153. "by_field_name": "mlcategory"
  154. }]
  155. },
  156. "data_description" : {
  157. }
  158. }
  159. --------------------------------------------------
  160. If you specify any part of the `categorization_analyzer`, however, any omitted
  161. sub-properties are _not_ set to default values.
  162. The `ml_classic` tokenizer and the day and month stopword filter are more or less
  163. equivalent to the following analyzer, which is defined using only built-in {es}
  164. {ref}/analysis-tokenizers.html[tokenizers] and
  165. {ref}/analysis-tokenfilters.html[token filters]:
  166. [source,console]
  167. ----------------------------------
  168. PUT _ml/anomaly_detectors/it_ops_new_logs3
  169. {
  170. "description" : "IT Ops Application Logs",
  171. "analysis_config" : {
  172. "categorization_field_name": "message",
  173. "bucket_span":"30m",
  174. "detectors" :[{
  175. "function":"count",
  176. "by_field_name": "mlcategory",
  177. "detector_description": "Unusual message counts"
  178. }],
  179. "categorization_analyzer":{
  180. "tokenizer": {
  181. "type" : "simple_pattern_split",
  182. "pattern" : "[^-0-9A-Za-z_.]+" <1>
  183. },
  184. "filter": [
  185. { "type" : "pattern_replace", "pattern": "^[0-9].*" }, <2>
  186. { "type" : "pattern_replace", "pattern": "^[-0-9A-Fa-f.]+$" }, <3>
  187. { "type" : "pattern_replace", "pattern": "^[^0-9A-Za-z]+" }, <4>
  188. { "type" : "pattern_replace", "pattern": "[^0-9A-Za-z]+$" }, <5>
  189. { "type" : "stop", "stopwords": [
  190. "",
  191. "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday",
  192. "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  193. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
  194. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  195. "GMT", "UTC"
  196. ] }
  197. ]
  198. }
  199. },
  200. "analysis_limits":{
  201. "categorization_examples_limit": 5
  202. },
  203. "data_description" : {
  204. "time_field":"time",
  205. "time_format": "epoch_ms"
  206. }
  207. }
  208. ----------------------------------
  209. // TEST[skip:needs-licence]
  210. <1> Tokens basically consist of hyphens, digits, letters, underscores and dots.
  211. <2> By default, categorization ignores tokens that begin with a digit.
  212. <3> By default, categorization also ignores tokens that are hexadecimal numbers.
  213. <4> Underscores, hyphens, and dots are removed from the beginning of tokens.
  214. <5> Underscores, hyphens, and dots are also removed from the end of tokens.
  215. The key difference between the default `categorization_analyzer` and this example
  216. analyzer is that using the `ml_classic` tokenizer is several times faster. The
  217. difference in behavior is that this custom analyzer does not include accented
  218. letters in tokens whereas the `ml_classic` tokenizer does, although that could
  219. be fixed by using more complex regular expressions.
  220. If you are categorizing non-English messages in a language where words are
  221. separated by spaces, you might get better results if you change the day or month
  222. words in the stop token filter to the appropriate words in your language. If you
  223. are categorizing messages in a language where words are not separated by spaces,
  224. you must use a different tokenizer as well in order to get sensible
  225. categorization results.
  226. It is important to be aware that analyzing for categorization of machine
  227. generated log messages is a little different from tokenizing for search.
  228. Features that work well for search, such as stemming, synonym substitution, and
  229. lowercasing are likely to make the results of categorization worse. However, in
  230. order for drill down from {ml} results to work correctly, the tokens that the
  231. categorization analyzer produces must be similar to those produced by the search
  232. analyzer. If they are sufficiently similar, when you search for the tokens that
  233. the categorization analyzer produces then you find the original document that
  234. the categorization field value came from.
  235. NOTE: To add the `categorization_analyzer` property in {kib}, you must use the
  236. **Edit JSON** tab and copy the `categorization_analyzer` object from one of the
  237. API examples above.
  238. [float]
  239. [[ml-viewing-categories]]
  240. ==== Viewing categorization results
  241. After you open the job and start the {dfeed} or supply data to the job, you can
  242. view the categorization results in {kib}. For example:
  243. [role="screenshot"]
  244. image::images/ml-category-anomalies.jpg["Categorization example in the Anomaly Explorer"]
  245. For this type of job, the **Anomaly Explorer** contains extra information for
  246. each anomaly: the name of the category (for example, `mlcategory 11`) and
  247. examples of the messages in that category. In this case, you can use these
  248. details to investigate occurrences of unusually high message counts for specific
  249. message categories.