logstash.js 4.2 KB

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