logstash.js 4.2 KB

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