procinfo 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/usr/bin/perl -w
  2. #
  3. # procinfo.pl script from waimea ( http://www.waimea.org/ ) ported to output
  4. # pekwm syntax by Claes Nasten ( http://pekdon.net/ )
  5. #
  6. # REPORTED to openbox3 pipe menu syntax by Dave Foster (daf@minuslab.net)
  7. # some functions with no path to execute were removed, otherwise, no
  8. # logic was changed in this script at all.
  9. # install instructions:
  10. # 1) Put script somewhere (~/.config/openbox/scripts/ works well)
  11. # 2) Make script executable (chmod +x procinfo.pl)
  12. # 3) Edit openbox menu file:
  13. # a) Add <menu id="proc-menu" label="Processes" execute="pathto/procinfo.pl" />
  14. # b) Add <menu id="proc-menu" somewhere on your root menu or wherever you want it.
  15. use POSIX;
  16. my $script_location = $0;
  17. my $offset = 0;
  18. my $proc_dir = "/proc";
  19. my $pid = 0;
  20. my $host = "";
  21. my $user = "";
  22. $user = $ENV{USER};
  23. chomp(my $hostname = qx(hostname));
  24. my $forcepid = 0;
  25. my $file = 0;
  26. my $list_length = 20;
  27. get_opt();
  28. print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  29. print "<openbox_pipe_menu>\n";
  30. if ($forcepid) {
  31. if ($hostname eq $host && $pid) {
  32. pid_info();
  33. } else {
  34. print "Name = \"process info\"\n";
  35. print "Entry = \"process info for this window could not be retrived\" { }\n";
  36. }
  37. } else {
  38. if ($pid) {
  39. pid_info();
  40. } else {
  41. list_proc();
  42. }
  43. }
  44. print "</openbox_pipe_menu>\n";
  45. sub list_proc {
  46. # my $cmdline = 0;
  47. my $proc_name = 0;
  48. my $i = $offset;
  49. my $owner = "";
  50. my $printed = 0;
  51. my @output;
  52. opendir(DIR, $proc_dir) || die "can't opendir $proc_dir: $!";
  53. @proc_list = grep { (/^[^\.].*/ || /^\.\..*/) && /^\d+$/} readdir(DIR);
  54. closedir DIR;
  55. # print "$_\n" foreach @proc_list;
  56. while (($pid = $proc_list[$i]) && $printed <= $list_length) {
  57. open(FILE, "$proc_dir\/$pid\/status") || die "can't opendir $proc_dir: $!";
  58. ($proc_name = (split(':', <FILE>))[1]) =~ s/^\s*(.*)\n/$1/;
  59. # if ($user eq
  60. while ($_ = <FILE>) {
  61. if (/^Uid\:\s*(\d+)\s+/) {
  62. $owner = getpwuid $1;
  63. }
  64. }
  65. close FILE;
  66. if (($user && ($user eq 'all' || $user eq 'ALL')) ||
  67. ($owner && $user && ($owner eq $user))) {
  68. if ($printed < $list_length) {
  69. # push @output, "Submenu = \"$pid - $proc_name\" { Entry { Actions = \"Dynamic $script_location -pid $proc_list[$i]\" } }\n";
  70. push @output, "<menu id=\"menu-proc-$pid\" label=\"$pid - $proc_name\" execute=\"$script_location -pid $proc_list[$i]\" />\n";
  71. }
  72. else {
  73. $i--;
  74. }
  75. $printed++;
  76. }
  77. $i++;
  78. }
  79. if ($proc_list[$i]) {
  80. $_ = $i;
  81. print "Submenu = \"more...\" { Entry { Actions = \"Dynamic $script_location -offset $_ -user $user\" } }\n";
  82. }
  83. print @output;
  84. }
  85. sub pid_info {
  86. my $name = "";
  87. my @procinfo;
  88. my $cmdline = "";
  89. my $priority = "";
  90. my $msize = "";
  91. my $mlck = "";
  92. my $mrss = "";
  93. my $mdata = "";
  94. my $mstk = "";
  95. my $mexe = "";
  96. my $mlib = "";
  97. my $pid_err = 0;
  98. open(FILE, "$proc_dir\/$pid\/stat") || pid_err();
  99. $_ = <FILE>;
  100. @procinfo = split(/ /,$_);
  101. close FILE;
  102. $procinfo[1] =~ m/^\((.*)\)/;
  103. $name = $1;
  104. $priority = $procinfo[18];
  105. open(FILE, "$proc_dir\/$pid\/status") || die "can't opendir $proc_dir: $!";
  106. while ($_ = <FILE>) {
  107. if ($_ =~ m/^State.*\((\w*)\)$/) {
  108. $state = $1;
  109. $state =~ s/\(/\\\(/;
  110. $state =~ s/\)/\\\)/;
  111. $state =~ s/\t/ /;
  112. $state =~ s/\s+/ /;
  113. }
  114. if ($_ =~ m/^VmSize/) {
  115. $_ =~ m/.*:\t*\s*(.*)$/;
  116. $msize = $1;
  117. }
  118. if ($_ =~ m/^VmLck/) {
  119. $_ =~ m/.*:\t*\s*(.*)$/;
  120. $mlck = $1;
  121. }
  122. if ($_ =~ m/^VmRSS/) {
  123. $_ =~ m/.*:\t*\s*(.*)$/;
  124. $mrss = $1;
  125. }
  126. if ($_ =~ m/^VmData/) {
  127. $_ =~ m/.*:\t*\s*(.*)$/;
  128. $mdata = $1;
  129. }
  130. if ($_ =~ m/^VmStk/) {
  131. $_ =~ m/.*:\t*\s*(.*)$/;
  132. $mstk = $1;
  133. }
  134. if ($_ =~ m/^VmExe/) {
  135. $_ =~ m/.*:\t*\s*(.*)$/;
  136. $mexe = $1;
  137. }
  138. if ($_ =~ m/^VmLib/) {
  139. $_ =~ m/.*:\t*\s*(.*)$/;
  140. $mlib = $1;
  141. }
  142. }
  143. if (open(FILE, "$proc_dir\/$pid\/cmdline")) {
  144. $_ = <FILE>;
  145. while ($_ =~ m/\0/) {
  146. $_ =~ s/\0/\\\" \\\"/;
  147. }
  148. $_ =~ m/(.*).{3}$/;
  149. $cmdline = "\\\"$1";
  150. close FILE;
  151. }
  152. print "\t<menu id=\"menu-proc-$pid-state\" label=\"state ($state)\">\n";
  153. print "\t\t<item label=\"stop\">\n";
  154. print "\t\t\t<action name=\"Execute\"><execute>kill -SIGSTOP $pid</execute></action>\n";
  155. print "\t\t</item>\n";
  156. print "\t\t<item label=\"continue\">\n";
  157. print "\t\t\t<action name=\"Execute\"><execute>kill -SIGCONT $pid</execute></action>\n";
  158. print "\t\t</item>\n";
  159. print "\t</menu\n>";
  160. print "\t<menu id=\"menu-proc-$pid-memory\" label=\"memory ($msize)\">\n";
  161. print "\t\t<item label=\"size: $msize\">\n";
  162. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  163. print "\t\t</item>\n";
  164. print "\t\t<item label=\"lck: $mlck\">\n";
  165. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  166. print "\t\t</item>\n";
  167. print "\t\t<item label=\"rss: $msize\">\n";
  168. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  169. print "\t\t</item>\n";
  170. print "\t\t<item label=\"data: $mdata\">\n";
  171. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  172. print "\t\t</item>\n";
  173. print "\t\t<item label=\"stk: $mstk\">\n";
  174. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  175. print "\t\t</item>\n";
  176. print "\t\t<item label=\"exe: $mexe\">\n";
  177. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  178. print "\t\t</item>\n";
  179. print "\t\t<item label=\"lib: $mlib\">\n";
  180. print "\t\t\t<action name=\"Execute\"><execute>true</execute></action>\n";
  181. print "\t\t</item>\n";
  182. print "\t</menu>\n";
  183. print "\t<menu id=\"menu-proc-$pid-priority\" label=\"priority ($priority)\">\n";
  184. print "\t\t<item label=\"increase by 1\">\n";
  185. print "\t\t\t<action name=\"Execute\"><execute>renice +1 $pid</execute></action>\n";
  186. print "\t\t</item>\n";
  187. print "\t\t<item label=\"0 (base)\">\n";
  188. print "\t\t\t<action name=\"Execute\"><execute>renice 0 $pid</execute></action>\n";
  189. print "\t\t</item>\n";
  190. print "\t\t<item label=\"5\">\n";
  191. print "\t\t\t<action name=\"Execute\"><execute>renice 5 $pid</execute></action>\n";
  192. print "\t\t</item>\n";
  193. print "\t\t<item label=\"10\">\n";
  194. print "\t\t\t<action name=\"Execute\"><execute>renice 10 $pid</execute></action>\n";
  195. print "\t\t</item>\n";
  196. print "\t\t<item label=\"15\">\n";
  197. print "\t\t\t<action name=\"Execute\"><execute>renice 15 $pid</execute></action>\n";
  198. print "\t\t</item>\n";
  199. print "\t\t<item label=\"20 (idle)\">\n";
  200. print "\t\t\t<action name=\"Execute\"><execute>renice 20 $pid</execute></action>\n";
  201. print "\t\t</item>\n";
  202. print "\t</menu>\n";
  203. print "\t<menu id=\"menu-proc-$pid-signal\" label=\"send signal\">\n";
  204. print "\t\t<item label=\"sighup\">\n";
  205. print "\t\t\t<action name=\"Execute\"><execute>kill -HUP $pid</execute></action>\n";
  206. print "\t\t</item>\n";
  207. print "\t\t<item label=\"sigint\">\n";
  208. print "\t\t\t<action name=\"Execute\"><execute>kill -INT $pid</execute></action>\n";
  209. print "\t\t</item>\n";
  210. print "\t\t<item label=\"sigkill\">\n";
  211. print "\t\t\t<action name=\"Execute\"><execute>kill -KILL $pid</execute></action>\n";
  212. print "\t\t</item>\n";
  213. print "\t</menu>\n";
  214. if ($cmdline ne "") {
  215. print "\t<item label=\"restart\">\n";
  216. print "\t\t<action name=\"Execute\"><execute>kill $pid &amp;&amp; $cmdline</execute></action>\n";
  217. print "\t</item>\n";
  218. print "\t<item label=\"spawn new\">\n";
  219. print "\t\t<action name=\"Execute\"><execute>$cmdline</execute></action>\n";
  220. print "\t</item>\n";
  221. }
  222. }
  223. sub get_opt {
  224. my $i = 0;
  225. while ($ARGV[$i+1] && ($_ = $ARGV[$i])) {
  226. if (/\-offset/) {
  227. $offset = $ARGV[$i+1];
  228. }
  229. if (/\-pid/) {
  230. $pid = $ARGV[$i+1];
  231. }
  232. if (/\-proc_dir/) {
  233. $proc_dir = $ARGV[$i+1];
  234. }
  235. if (/\-user/) {
  236. $user = $ARGV[$i+1];
  237. }
  238. if (/\-host/) {
  239. $host = $ARGV[$i+1];
  240. }
  241. $i++;
  242. }
  243. $i = 0;
  244. while ($_ = $ARGV[$i]) {
  245. $i++;
  246. if (/\-host/) {
  247. $forcepid = 1;
  248. }
  249. }
  250. }
  251. sub pid_err {
  252. print "<item label=\"??? ($pid)\">\n";
  253. print "<action name=\"Execute\"><execute>true</execute></action>\n";
  254. print "</item>\n";
  255. exit;
  256. }