logstash.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* global _, kbn */
  2. /*
  3. * Complex scripted Logstash dashboard
  4. * This script generates a dashboard object that Kibana can load. It also takes a number of user
  5. * supplied URL parameters, none are required:
  6. *
  7. * index :: Which index to search? If this is specified, interval is set to 'none'
  8. * pattern :: Does nothing if index is specified. Set a timestamped index pattern. Default: [logstash-]YYYY.MM.DD
  9. * interval :: Sets the index interval (eg: day,week,month,year), Default: day
  10. *
  11. * split :: The character to split the queries on Default: ','
  12. * query :: By default, a comma seperated list of queries to run. Default: *
  13. *
  14. * from :: Search this amount of time back, eg 15m, 1h, 2d. Default: 15m
  15. * timefield :: The field containing the time to filter on, Default: @timestamp
  16. *
  17. * fields :: comma seperated list of fields to show in the table
  18. * sort :: comma seperated field to sort on, and direction, eg sort=@timestamp,desc
  19. *
  20. */
  21. 'use strict';
  22. // Setup some variables
  23. var dashboard, queries, _d_timespan;
  24. // All url parameters are available via the ARGS object
  25. var ARGS;
  26. // Set a default timespan if one isn't specified
  27. _d_timespan = '1h';
  28. // Intialize a skeleton with nothing but a rows array and service object
  29. dashboard = {
  30. rows : [],
  31. services : {}
  32. };
  33. // Set a title
  34. dashboard.title = 'Logstash Search';
  35. // Allow the user to set the index, if they dont, fall back to logstash.
  36. if(!_.isUndefined(ARGS.index)) {
  37. dashboard.index = {
  38. default: ARGS.index,
  39. interval: 'none'
  40. };
  41. } else {
  42. // Don't fail to default
  43. dashboard.failover = false;
  44. dashboard.index = {
  45. default: ARGS.index||'ADD_A_TIME_FILTER',
  46. pattern: ARGS.pattern||'[logstash-]YYYY.MM.DD',
  47. interval: ARGS.interval||'day'
  48. };
  49. }
  50. // In this dashboard we let users pass queries as comma seperated list to the query parameter.
  51. // Or they can specify a split character using the split aparameter
  52. // If query is defined, split it into a list of query objects
  53. // NOTE: ids must be integers, hence the parseInt()s
  54. if(!_.isUndefined(ARGS.query)) {
  55. queries = _.object(_.map(ARGS.query.split(ARGS.split||','), function(v,k) {
  56. return [k,{
  57. query: v,
  58. id: parseInt(k,10),
  59. alias: v
  60. }];
  61. }));
  62. } else {
  63. // No queries passed? Initialize a single query to match everything
  64. queries = {
  65. 0: {
  66. query: '*',
  67. id: 0
  68. }
  69. };
  70. }
  71. // Now populate the query service with our objects
  72. dashboard.services.query = {
  73. list : queries,
  74. ids : _.map(_.keys(queries),function(v){return parseInt(v,10);})
  75. };
  76. // Lets also add a default time filter, the value of which can be specified by the user
  77. // This isn't strictly needed, but it gets rid of the info alert about the missing time filter
  78. dashboard.services.filter = {
  79. list: {
  80. 0: {
  81. from: kbn.time_ago(ARGS.from||_d_timespan),
  82. to: new Date(),
  83. field: ARGS.timefield||"@timestamp",
  84. type: "time",
  85. active: true,
  86. id: 0
  87. }
  88. },
  89. ids: [0]
  90. };
  91. // Ok, lets make some rows. The Filters row is collapsed by default
  92. dashboard.rows = [
  93. {
  94. title: "Time span",
  95. height: "30px"
  96. },
  97. {
  98. title: "Query",
  99. height: "30px"
  100. },
  101. {
  102. title: "Filters",
  103. height: "100px",
  104. collapse: true
  105. },
  106. {
  107. title: "Chart",
  108. height: "300px"
  109. },
  110. {
  111. title: "Events",
  112. height: "400px"
  113. }
  114. ];
  115. // Setup some panels. A query panel and a filter panel on the same row
  116. dashboard.rows[0].panels = [
  117. {
  118. title: "Set time filter",
  119. type: 'timepicker',
  120. span: 6,
  121. timespan: ARGS.from||_d_timespan
  122. }
  123. ];
  124. // Add a filtering panel to the 3rd row
  125. dashboard.rows[1].panels = [
  126. {
  127. title: 'search',
  128. type: 'query',
  129. span: 12
  130. }
  131. ];
  132. // Add a filtering panel to the 3rd row
  133. dashboard.rows[2].panels = [
  134. {
  135. title: 'filters (applied globally)',
  136. type: 'filtering',
  137. span: 12
  138. }
  139. ];
  140. // And a histogram that allows the user to specify the interval and time field
  141. dashboard.rows[3].panels = [
  142. {
  143. title: 'events over time',
  144. type: 'histogram',
  145. time_field: ARGS.timefield||"@timestamp",
  146. auto_int: true,
  147. span: 12
  148. }
  149. ];
  150. // And a table row where you can specify field and sort order
  151. dashboard.rows[4].panels = [
  152. {
  153. title: 'all events',
  154. type: 'table',
  155. fields: !_.isUndefined(ARGS.fields) ? ARGS.fields.split(',') : [],
  156. sort: !_.isUndefined(ARGS.sort) ? ARGS.sort.split(',') : [ARGS.timefield||'@timestamp','desc'],
  157. overflow: 'expand',
  158. span: 12
  159. }
  160. ];
  161. // Now return the object and we're good!
  162. return dashboard;