Skip to content

Commit d8f54ee

Browse files
committed
csysdig view actions infrastructure
1 parent 3981186 commit d8f54ee

File tree

8 files changed

+203
-27
lines changed

8 files changed

+203
-27
lines changed

userspace/libsinsp/chisel.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,13 @@ void sinsp_chisel::parse_view_columns(lua_State *ls, OUT chisel_desc* cd, OUT vo
553553
}
554554
}
555555

556-
void sinsp_chisel::parse_view_hotkey(lua_State *ls, OUT chisel_desc* cd, OUT void* hotkeys)
556+
void sinsp_chisel::parse_view_action(lua_State *ls, OUT chisel_desc* cd, OUT void* actions)
557557
{
558-
vector<sinsp_view_hotkey_info>* keys = (vector<sinsp_view_hotkey_info>*)hotkeys;
558+
vector<sinsp_view_action_info>* keys = (vector<sinsp_view_action_info>*)actions;
559559

560560
lua_pushnil(ls);
561561

562-
char key;
562+
char key = 0;
563563
string command;
564564
string description;
565565
string tmpstr;
@@ -568,7 +568,7 @@ void sinsp_chisel::parse_view_hotkey(lua_State *ls, OUT chisel_desc* cd, OUT voi
568568
{
569569
string fldname = lua_tostring(ls, -2);
570570

571-
if(fldname == "key")
571+
if(fldname == "hotkey")
572572
{
573573
tmpstr = lua_tostring(ls, -1);
574574
if(tmpstr.size() == 1)
@@ -577,7 +577,7 @@ void sinsp_chisel::parse_view_hotkey(lua_State *ls, OUT chisel_desc* cd, OUT voi
577577
}
578578
else
579579
{
580-
throw sinsp_exception("hotkey 'key' fileld must be a single character string");
580+
throw sinsp_exception("action 'key' field must be a single character string");
581581
}
582582
}
583583
else if(fldname == "command")
@@ -592,12 +592,22 @@ void sinsp_chisel::parse_view_hotkey(lua_State *ls, OUT chisel_desc* cd, OUT voi
592592
lua_pop(ls, 1);
593593
}
594594

595-
keys->push_back(sinsp_view_hotkey_info(key,
595+
if(key == 0)
596+
{
597+
throw sinsp_exception("action missing the 'key' value");
598+
}
599+
600+
if(command == "")
601+
{
602+
throw sinsp_exception("action missing the 'command' value");
603+
}
604+
605+
keys->push_back(sinsp_view_action_info(key,
596606
command,
597607
description));
598608
}
599609

600-
void sinsp_chisel::parse_view_hotkeys(lua_State *ls, OUT chisel_desc* cd, OUT void* hotkeys)
610+
void sinsp_chisel::parse_view_actions(lua_State *ls, OUT chisel_desc* cd, OUT void* actions)
601611
{
602612
string name;
603613
string type;
@@ -609,11 +619,11 @@ void sinsp_chisel::parse_view_hotkeys(lua_State *ls, OUT chisel_desc* cd, OUT vo
609619
{
610620
if(lua_istable(ls, -1))
611621
{
612-
parse_view_hotkey(ls, cd, hotkeys);
622+
parse_view_action(ls, cd, actions);
613623
}
614624
else
615625
{
616-
throw sinsp_exception("view_info hotkey entries must be tables");
626+
throw sinsp_exception("view_info action entries must be tables");
617627
}
618628

619629
lua_pop(ls, 1);
@@ -640,7 +650,7 @@ bool sinsp_chisel::parse_view_info(lua_State *ls, OUT chisel_desc* cd)
640650
bool use_defaults = false;
641651
sinsp_view_info::viewtype vt = sinsp_view_info::T_TABLE;
642652
vector<sinsp_view_column_info> columns;
643-
vector<sinsp_view_hotkey_info> hotkeys;
653+
vector<sinsp_view_action_info> actions;
644654
vector<string> tags;
645655
vector<string> tips;
646656
string drilldown_target;
@@ -798,11 +808,11 @@ bool sinsp_chisel::parse_view_info(lua_State *ls, OUT chisel_desc* cd)
798808
throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table");
799809
}
800810
}
801-
else if(fldname == "hotkeys")
811+
else if(fldname == "actions")
802812
{
803813
if(lua_istable(ls, -1))
804814
{
805-
parse_view_hotkeys(ls, cd, &hotkeys);
815+
parse_view_actions(ls, cd, &actions);
806816
}
807817
else
808818
{
@@ -825,7 +835,7 @@ bool sinsp_chisel::parse_view_info(lua_State *ls, OUT chisel_desc* cd)
825835
drilldown_target,
826836
use_defaults,
827837
is_root,
828-
hotkeys);
838+
actions);
829839

830840
return true;
831841
}

userspace/libsinsp/chisel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class SINSP_PUBLIC sinsp_chisel
133133
static sinsp_field_aggregation string_to_aggregation(string ag);
134134
static void parse_view_column(lua_State *ls, OUT chisel_desc* cd, OUT void* columns);
135135
static void parse_view_columns(lua_State *ls, OUT chisel_desc* cd, OUT void* columns);
136-
static void parse_view_hotkey(lua_State *ls, OUT chisel_desc* cd, OUT void* hotkeys);
137-
static void parse_view_hotkeys(lua_State *ls, OUT chisel_desc* cd, OUT void* hotkeys);
136+
static void parse_view_action(lua_State *ls, OUT chisel_desc* cd, OUT void* actions);
137+
static void parse_view_actions(lua_State *ls, OUT chisel_desc* cd, OUT void* actions);
138138
static bool parse_view_info(lua_State *ls, OUT chisel_desc* cd);
139139
static bool init_lua_chisel(chisel_desc &cd, string const &path);
140140
void first_event_inits(sinsp_evt* evt);

userspace/libsinsp/cursestable.cpp

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,42 @@ void curses_table::render(bool data_changed)
529529
m_parent->render();
530530
refresh();
531531
}
532-
532+
533+
string curses_table::get_field_val(string fldname)
534+
{
535+
uint32_t j;
536+
vector<sinsp_table_field>* row;
537+
string res;
538+
539+
row = &(m_data->at(m_selct).m_values);
540+
541+
for(j = 1; j < m_parent->m_datatable->m_postmerge_legend.size(); j++)
542+
{
543+
auto le = m_parent->m_datatable->m_postmerge_legend[j];
544+
545+
if(le.m_name == fldname)
546+
{
547+
uint32_t k = j - 1;
548+
m_converter->set_val(m_legend[k].m_info.m_type,
549+
row->at(k).m_val,
550+
row->at(k).m_len,
551+
row->at(k).m_cnt,
552+
m_legend[k].m_info.m_print_format);
553+
554+
res = m_converter->tostring_nice(NULL, 0, 0);
555+
556+
break;
557+
}
558+
}
559+
560+
if(j == m_parent->m_datatable->m_postmerge_legend.size())
561+
{
562+
throw sinsp_exception("field: " + fldname + " not found in this view");
563+
}
564+
565+
return res;
566+
}
567+
533568
//
534569
// Return false if the user wants us to exit
535570
//
@@ -540,6 +575,7 @@ sysdig_table_action curses_table::handle_input(int ch)
540575
return STA_PARENT_HANDLE;
541576
}
542577

578+
543579
switch(ch)
544580
{
545581
/*
@@ -701,6 +737,117 @@ sysdig_table_action curses_table::handle_input(int ch)
701737
break;
702738
}
703739

740+
//
741+
// Check if this view has any action configured, and if yes find if this key
742+
// is one of the view hotkeys
743+
//
744+
sinsp_view_info* vinfo = m_parent->get_selected_view();
745+
746+
for(auto hk : vinfo->m_actions)
747+
{
748+
if(hk.m_hotkey == ch)
749+
{
750+
string resolved_command;
751+
bool replacing = false;
752+
string fld_to_replace;
753+
754+
//
755+
// Scan the command string and replace the field names with the values from the selection
756+
//
757+
for(uint32_t j = 0; j < hk.m_command.size(); j++)
758+
{
759+
char sc = hk.m_command[j];
760+
761+
if(sc == '%')
762+
{
763+
fld_to_replace = "";
764+
765+
if(replacing)
766+
{
767+
throw sinsp_exception("the following command has the wrong syntax: " + hk.m_command);
768+
}
769+
770+
replacing = true;
771+
}
772+
else
773+
{
774+
if(replacing)
775+
{
776+
if(sc == ' ' || sc == '\t' || sc == '0')
777+
{
778+
replacing = false;
779+
string val = get_field_val(fld_to_replace);
780+
resolved_command += val;
781+
resolved_command += sc;
782+
}
783+
else
784+
{
785+
fld_to_replace += sc;
786+
}
787+
}
788+
else
789+
{
790+
resolved_command += sc;
791+
}
792+
}
793+
}
794+
795+
if(replacing)
796+
{
797+
string val = get_field_val(fld_to_replace);
798+
resolved_command += val;
799+
}
800+
801+
g_logger.format("running command: %s", resolved_command.c_str());
802+
803+
sinsp_table_field* rowkey = m_parent->m_datatable->get_row_key(m_selct);
804+
/*
805+
curs_set(1);
806+
clear();
807+
move(1, 0);
808+
refresh();
809+
810+
reset_shell_mode();
811+
*/
812+
//
813+
// Exit curses mode
814+
//
815+
endwin();
816+
817+
//
818+
// Run the command
819+
//
820+
int sret = system(resolved_command.c_str());
821+
if(sret == -1)
822+
{
823+
g_logger.format("command failed");
824+
}
825+
826+
printf("Command finished. Press enter to return to csysdig.");
827+
fflush(stdout);
828+
829+
//
830+
// Wait for the enter key
831+
//
832+
while(getch() == -1)
833+
{
834+
usleep(10000);
835+
}
836+
837+
//
838+
// Empty the keyboard buffer
839+
//
840+
while(getch() != -1);
841+
842+
//
843+
// Reenter curses mode
844+
//
845+
reset_prog_mode();
846+
847+
return STA_NONE;
848+
}
849+
}
850+
704851
return STA_PARENT_HANDLE;
705852
}
706853

userspace/libsinsp/cursestable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class curses_table :
5757
alignment get_field_alignment(ppm_param_type type);
5858
void print_error(string wstr);
5959
void print_wait();
60+
string get_field_val(string fldname);
6061

6162
sinsp* m_inspector;
6263
WINDOW* m_tblwin;

userspace/libsinsp/viewinfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sinsp_view_info::sinsp_view_info(viewtype type,
4545
string drilldown_target,
4646
bool use_defaults,
4747
bool is_root,
48-
vector<sinsp_view_hotkey_info> hotkeys)
48+
vector<sinsp_view_action_info> actions)
4949
{
5050
m_id = id;
5151
m_name = name;
@@ -74,7 +74,7 @@ sinsp_view_info::sinsp_view_info(viewtype type,
7474

7575
m_filter = filter;
7676
m_valid = true;
77-
m_hotkeys = hotkeys;
77+
m_actions = actions;
7878
}
7979

8080
void sinsp_view_info::set_sorting_col()

userspace/libsinsp/viewinfo.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ class sinsp_view_column_info
7575
};
7676

7777
///////////////////////////////////////////////////////////////////////////////
78-
// Hotkey information
78+
// action information
7979
///////////////////////////////////////////////////////////////////////////////
80-
class sinsp_view_hotkey_info
80+
class sinsp_view_action_info
8181
{
8282
public:
83-
sinsp_view_hotkey_info(char key,
83+
sinsp_view_action_info(char hotkey,
8484
string command,
8585
string description)
8686
{
87-
m_key = key;
87+
m_hotkey = hotkey;
8888
m_command = command;
8989
m_description = description;
9090
}
9191

92-
char m_key;
92+
char m_hotkey;
9393
string m_command;
9494
string m_description;
9595
};
@@ -121,7 +121,7 @@ class sinsp_view_info
121121
string drilldown_target,
122122
bool use_defaults,
123123
bool is_root,
124-
vector<sinsp_view_hotkey_info> hotkeys);
124+
vector<sinsp_view_action_info> actions);
125125

126126
void get_col_names_and_sizes(OUT vector<string>* colnames, OUT vector<int32_t>* colsizes);
127127
viewtype get_type()
@@ -151,7 +151,7 @@ class sinsp_view_info
151151
bool m_valid;
152152
string m_drilldown_target;
153153
bool m_is_root;
154-
vector<sinsp_view_hotkey_info> m_hotkeys;
154+
vector<sinsp_view_action_info> m_actions;
155155

156156
private:
157157
void set_sorting_col();

userspace/sysdig/chisels/v_containers.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,23 @@ view_info =
113113
description = "Name of the container.",
114114
colsize = 0
115115
},
116-
}
116+
},
117+
actions =
118+
{
119+
{
120+
hotkey = "a",
121+
command = "docker attach %container.id",
122+
description = "attach"
123+
},
124+
{
125+
hotkey = "l",
126+
command = "docker logs %container.id",
127+
description = "logs"
128+
},
129+
{
130+
hotkey = "b",
131+
command = "docker exec -i -t %container.id /bin/bash",
132+
description = "logs"
133+
},
134+
},
117135
}

userspace/sysdig/chisels/v_procs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,5 @@ view_info =
120120
aggregation = "MAX",
121121
colsize = 0
122122
}
123-
}
123+
},
124124
}

0 commit comments

Comments
 (0)