"Fossies" - the Fresh Open Source Software Archive

Member "sysdig-0.26.1/userspace/sysdig/chisels/around.lua" (24 May 2019, 3638 Bytes) of package /linux/misc/sysdig-0.26.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 --[[
    2 Copyright (C) 2013-2018 Draios Inc dba Sysdig.
    3 
    4 This file is part of sysdig.
    5 
    6 Licensed under the Apache License, Version 2.0 (the "License");
    7 you may not use this file except in compliance with the License.
    8 You may obtain a copy of the License at
    9 
   10     http://www.apache.org/licenses/LICENSE-2.0
   11 
   12 Unless required by applicable law or agreed to in writing, software
   13 distributed under the License is distributed on an "AS IS" BASIS,
   14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15 See the License for the specific language governing permissions and
   16 limitations under the License.
   17 
   18 --]]
   19 
   20 -- Chisel description
   21 description = "Given a filter on the command line, this chisel saves the events that are in a time range around filter matches, and that are on the SAME process/thread. The time range can be adjusted with the dump_range_ms argument. For example, 'sysdig -c around evt.type=open and evt.failed=true' will save two seconds of activity around every failed open.";
   22 short_description = "Export to file the events around the time range where the given filter matches.";
   23 category = "Misc";
   24         
   25 -- Argument list
   26 args =
   27 {
   28     {
   29         name = "dump_file_name",
   30         description = "The name of the file where the chisel will write the events related to each syslog entry.",
   31         argtype = "string",
   32         optional = false
   33     },
   34     {
   35         name = "dump_range_ms",
   36         description = "The time interval to capture *before* and *after* each event, in milliseconds. For example, 500 means that 1 second around each displayed event (.5s before and .5s after) will be saved to <dump_file_name>. The default value for dump_range_ms is 1000.",
   37         argtype = "int",
   38         optional = true
   39     },
   40 }
   41 -- Imports and globals
   42 require "common"
   43 terminal = require "ansiterminal"
   44 terminal.enable_color(true)
   45 local dump_file_name = nil
   46 local dump_range_ms = "1000"
   47 local entrylist = {}
   48 local capturing = false
   49 
   50 -- Argument notification callback
   51 function on_set_arg(name, val)
   52     if name == "dump_file_name" then
   53         dump_file_name = val
   54         return true
   55     elseif name == "dump_range_ms" then
   56         dump_range_ms = val
   57         return true
   58     end
   59 
   60     return false
   61 end
   62 
   63 -- Initialization callback
   64 function on_init()  
   65     -- Request the fields that we need
   66     fpname = chisel.request_field("proc.name")
   67     ftid = chisel.request_field("thread.tid")
   68     fetime = chisel.request_field("evt.time")
   69 
   70     is_tty = sysdig.is_tty()
   71     
   72     if sysdig.get_filter() == "" then
   73         print("no filter specified")
   74         return false
   75     end
   76     
   77     return true
   78 end
   79 
   80 -- Final chisel initialization
   81 function on_capture_start()
   82     if sysdig.is_live() then
   83         print("live capture not supported")
   84         return false
   85     end
   86     
   87     capturing = true
   88 
   89     return true
   90 end
   91 
   92 -- Event parsing callback
   93 function on_event() 
   94     -- Extract the event details
   95     local pname = evt.field(fpname)
   96     local tid = evt.field(ftid)
   97     local etime = evt.field(fetime)
   98     
   99     if pname == nil then
  100         pname = "<NA>"
  101     end
  102     
  103     print(etime .. " " .. pname .. "(" .. tid .. ")")
  104     
  105     local hi, low = evt.get_ts()
  106     local tid = evt.field(ftid)
  107     table.insert(entrylist, {hi, low, tid})
  108 
  109     return true
  110 end
  111 
  112 -- Called by the engine at the end of the capture (Ctrl-C)
  113 function on_capture_end()
  114     if is_tty then
  115         print(terminal.reset)
  116     end
  117 
  118     if capturing then
  119         local sn = sysdig.get_evtsource_name()
  120 
  121         local args = "-F -r" .. sn .. " -w" .. dump_file_name .. " "
  122         
  123         for i, v in ipairs(entrylist) do
  124             if i ~= 1 then
  125                 args = args .. " or "
  126             end
  127             
  128             args = args .. "(evt.around[" .. ts_to_str(v[1], v[2]) .. "]=" .. dump_range_ms .. " and thread.tid=" .. v[3] .. ")"
  129         end     
  130 
  131         print("\nSaving events around " .. #entrylist .. " syslog entries to " .. dump_file_name)
  132         sysdig.run_sysdig(args)
  133     end
  134 end