"Fossies" - the Fresh Open Source Software Archive

Member "sysdig-0.26.1/userspace/sysdig/chisels/ps.lua" (24 May 2019, 2794 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 = "List the running processes, with an output that is similar to the one of ps. Output is at a point in time; adjust this in the filter. It defaults to time of evt.num=0";
   22 short_description = "List (and optionally filter) the machine processes.";
   23 category = "System State";
   24         
   25 -- Argument list
   26 args =
   27 {
   28     {
   29         name = "filter",
   30         description = "A sysdig-like filter expression that allows restricting the FD list. For example 'fd.name contains /etc' shows all the processes that have files open under /etc.",
   31         argtype = "filter",
   32         optional = true
   33     }
   34 }
   35 
   36 -- Argument initialization Callback
   37 function on_set_arg(name, val)
   38     if name == "filter" then
   39         filter = val
   40         return true
   41     end
   42 
   43     return false
   44 end
   45 
   46 -- Imports and globals
   47 require "common"
   48 local dctable = {}
   49 local capturing = false
   50 local filter = nil
   51 local match = false
   52 
   53 -- Argument notification callback
   54 function on_set_arg(name, val)
   55     if name == "filter" then
   56         filter = val
   57         return true
   58     end
   59 
   60     return false
   61 end
   62 
   63 -- Initialization callback
   64 function on_init()
   65     return true
   66 end
   67 
   68 function on_capture_start() 
   69     capturing = true
   70     return true
   71 end
   72 
   73 -- Event parsing callback
   74 function on_event()
   75     sysdig.end_capture()
   76     match = true
   77     return false
   78 end
   79 
   80 -- Called by the engine at the end of the capture (Ctrl-C)
   81 function on_capture_end(ts_s, ts_ns, delta)
   82     if not capturing then
   83         return
   84     end
   85     
   86     if match == false then
   87         print("empty capture or no event matching the filter")
   88         return
   89     end
   90     
   91     local ttable = sysdig.get_thread_table(filter)
   92 
   93     local sorted_ttable = pairs_top_by_val(ttable, 0, function(t,a,b) return a < b end)
   94     
   95     print(extend_string("TID", 8) ..
   96         extend_string("PID", 8) ..
   97         extend_string("USER", 12) ..
   98         extend_string("VIRT", 11) ..
   99         extend_string("RES", 11) ..
  100         extend_string("FDLIMIT", 10) ..
  101         extend_string("CMD", 20))
  102 
  103     for tid, proc in sorted_ttable do
  104         print(extend_string(tostring(tid), 8) ..
  105             extend_string(tostring(proc.pid), 8) ..
  106             extend_string(proc.username, 12) ..
  107             extend_string(format_bytes(proc.vmsize_kb * 1024), 11) ..
  108             extend_string(format_bytes(proc.vmrss_kb * 1024), 11) ..
  109             extend_string(tostring(proc.fdlimit), 10) ..
  110             proc.comm
  111             )
  112     end
  113 end