Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # Simple munin-node. Should be run from inetd or similar.
  4. #
  5. # This should be as small and elegant as possible, to make for easier
  6. # reviewing by sysadmins that want to do simple munin-stuff in high
  7. # security environments.
  8. #
  9. # $Log$
  10. # Revision 1.2 2004/01/15 15:20:01 jimmyo
  11. # Making things workable after name change. Upping for test verwion.
  12. #
  13. # Revision 1.1 2004/01/02 18:50:00 jimmyo
  14. # Renamed occurrances of lrrd -> munin
  15. #
  16. # Revision 1.1.1.1 2004/01/02 15:18:06 jimmyo
  17. # Import of LRRD CVS tree after renaming to Munin
  18. #
  19. # Revision 1.3 2003/11/24 10:15:56 jimmyo
  20. # Ready for 0.9.9 release
  21. #
  22. # Revision 1.2 2003/11/19 17:26:30 jimmyo
  23. # Now works with netcat
  24. #
  25. # Revision 1.1 2003/11/19 17:09:46 jimmyo
  26. # New mini-client. Not finished yet (only talks on stdout right now).
  27. #
  28. #
  29.  
  30. #use strict;
  31.  
  32. $| = 1;
  33.  
  34. my $clientdir = "/home/opt/etc/munin/plugins";
  35. my $conffile = "/home/opt/etc/munin/munin-node.conf";
  36. my $version = "1.2.5";
  37.  
  38. # Empty environment
  39. %ENV = ();
  40. $ENV{PATH} = "/bin:/usr/bin";
  41.  
  42. my $config = &read_config($conffile);
  43. my $plugins = &read_plugin_dir($clientdir);
  44.  
  45. my $hostname;
  46. if (defined $config->{hostname})
  47. {
  48. $hostname = $config->{hostname};
  49. }
  50. elsif (defined $config->{host_name})
  51. {
  52. $hostname = $config->{host_name};
  53. }
  54. else
  55. {
  56. $hostname = `hostname`;
  57. chomp $hostname;
  58. }
  59.  
  60. print "# munin node at $hostname\n";
  61.  
  62. while (<>)
  63. {
  64. if (/^\s*list(?:\s+\S+|)\s*$/i)
  65. { # Print plugin list
  66. print (join (' ', keys (%{$plugins})), "\n");
  67. }
  68. elsif (/^\s*version\s*/i)
  69. { # Print version number
  70. print "# munin node simple on $hostname version: $version\n";
  71. }
  72. elsif (/^\s*quit\s*/i or /^\s*\.\s*$/)
  73. { # Drop out
  74. exit (0);
  75. }
  76. elsif (/^\s*config\s+([A-Za-z0-9_]+)\s*$/i)
  77. {
  78. print &run_file ($plugins->{$1}, "config");
  79. print ".\n";
  80. }
  81. elsif (/^\s*fetch\s+([A-Za-z0-9_]+)\s*$/i)
  82. {
  83. print &run_file ($plugins->{$1});
  84. print ".\n";
  85. }
  86. else
  87. {
  88. print "# Unknown command. Try list, config, fetch, version or quit\n";
  89. }
  90. }
  91.  
  92. exit (0);
  93.  
  94. sub read_plugin_dir
  95. {
  96. my $cdir = shift;
  97. my $ret = {};
  98.  
  99. # Open and read the names of the files in the plugin directory
  100. if (-d $cdir)
  101. {
  102. opendir (DIR, $clientdir) or die "Could not open dir \"$cdir\" for reading: $!";
  103. my @files = grep {!/[^A-Za-z0-9_]/} readdir (DIR); # No funky chars
  104. closedir (DIR);
  105.  
  106. foreach my $file (@files)
  107. {
  108. $ret->{$file} = "$cdir/$file";
  109. }
  110. }
  111.  
  112. return $ret;
  113. }
  114.  
  115. sub read_config
  116. {
  117. my $file = shift;
  118. my $ret = {};
  119.  
  120. if (-f $file)
  121. {
  122. if (open (CONF, $file))
  123. {
  124. while (<CONF>)
  125. {
  126. chomp;
  127. s/#.*//; # Skip comments
  128. next unless /\S/; # Skip empty lines
  129.  
  130. if (/^\s*([a-zA-Z0-9_]+)\s+(\S+)$/)
  131. {
  132. $ret->{$1} = $2;
  133. }
  134. else
  135. {
  136. warn "Warning: unreadable configuration line in \"$file\": \"$_\".";
  137. }
  138. }
  139. close (CONF);
  140. }
  141. }
  142. return $ret;
  143. }
  144.  
  145. sub run_file
  146. {
  147. my $file = shift;
  148. my $arg = shift;
  149.  
  150. if (!defined $file or ! -e $file)
  151. {
  152. return "# Plugin does not exist.\n.\n";
  153. }
  154.  
  155. my $pid = open (CHILD, "-|");
  156.  
  157. if (!defined $pid) # Something went wrong
  158. {
  159. return "# Fork error: $!\n.\n";
  160. }
  161. elsif ($pid) # Parent
  162. {
  163. my $tmp = join ('', <CHILD>);
  164. close (CHILD);
  165. if ($?)
  166. {
  167. return "# Error, plugin exited with status $?.\n";
  168. }
  169. else
  170. {
  171. return $tmp;
  172. }
  173. }
  174. else # Child
  175. {
  176. if (defined $arg)
  177. {
  178. # print "# Executing: \"$file\" \"$arg\".\n";
  179. exec $file $file, $arg;
  180. }
  181. else
  182. {
  183. # print "# Executing: \"$file\".\n";
  184. exec $file $file;
  185. }
  186. }
  187. # notreached
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement