SymuxClient.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # $Id: SymuxClient.pm,v 1.11 2005/10/16 15:26:50 dijkstra Exp $
  2. #
  3. # Copyright (c) 2001-2005 Willem Dijkstra
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # - Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # - Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following
  14. # disclaimer in the documentation and/or other materials provided
  15. # with the distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. package SymuxClient;
  31. use Carp;
  32. use IO::Socket;
  33. my $streamitem =
  34. {cpu => {user => 1, nice => 2, system => 3, interrupt => 4, idle => 5},
  35. mem => {real_active => 1, real_total => 2, free => 3, swap_used => 4,
  36. swap_total =>5},
  37. if => {packets_in => 1, packets_out => 2, bytes_in => 3, bytes_out => 4,
  38. multicasts_in => 5, multicasts_out => 6, errors_in => 7,
  39. errors_out => 8, collisions => 9, drops => 10},
  40. io1 => {total_transfers => 1, total_seeks => 2, total_bytes => 3},
  41. pf => {bytes_v4_in => 1, bytes_v4_out => 2, bytes_v6_in => 3,
  42. bytes_v6_out => 4, packets_v4_in_pass => 5,
  43. packets_v4_in_drop => 6, packets_v4_out_pass => 7,
  44. packets_v4_out_drop => 8, packets_v6_in_pass => 9,
  45. packets_v6_in_drop => 10, packets_v6_out_pass => 11,
  46. packets_v6_out_drop => 12, states_entries => 13,
  47. states_searches => 14, states_inserts => 15,
  48. states_removals => 16, counters_match => 17,
  49. counters_badoffset => 18, counters_fragment => 19,
  50. counters_short => 20, counters_normalize => 21,
  51. counters_memory => 22},
  52. debug => {debug0 => 1, debug1 => 2, debug3 => 3, debug4 => 4, debug5 => 5,
  53. debug6 => 6, debug7 => 7, debug8 => 8, debug9 => 9,
  54. debug10 => 10, debug11 => 11, debug12 => 12, debug13 => 13,
  55. debug14 => 14, debug15 => 15, debug16 => 16, debug17 => 17,
  56. debug18 => 18, debug19 => 19},
  57. proc => {number => 1, uticks => 2, sticks => 3, iticks => 4, cpusec => 5,
  58. cpupct => 6, procsz => 7, rsssz => 8},
  59. mbuf => {totmbufs => 1, mt_data => 2, mt_oobdata => 3, mt_control => 4,
  60. mt_header => 5, mt_ftable => 6, mt_soname => 7, mt_soopts => 8,
  61. pgused => 9, pgtotal => 10, totmem => 11, totpct => 12,
  62. m_drops => 13, m_wait => 14, m_drain => 15 },
  63. sensor => {value => 1},
  64. io => {total_rxfers => 1, total_wxfers => 2, total_seeks => 3,
  65. total_rbytes => 4, total_rbytes => 5 },
  66. pfq => {sent_bytes => 1, sent_packets => 2, drop_bytes => 3,
  67. drop_packets => 4}
  68. df => {blocks => 1, bfree => 2, bavail => 3, files => 4, ffree => 5,
  69. syncwrites => 6, asyncwrites => 7}};
  70. sub new {
  71. my ($class, %arg) = @_;
  72. my $self;
  73. (defined $arg{host} && defined $arg{port}) or
  74. croak "error: need a host and a port to connect to.";
  75. ($self->{host}, $self->{port}) = ($arg{host}, $arg{port});
  76. $self->{retry} = (defined $arg{retry}? $arg{retry} : 10);
  77. bless($self, $class);
  78. $self->connect();
  79. return $self;
  80. }
  81. sub DESTROY {
  82. my $self = shift;
  83. if (defined $self->{sock}) {
  84. close($self->{sock});
  85. }
  86. }
  87. sub connect {
  88. my $self = shift;
  89. if (defined $self->{sock} && $self->{sock}->connected() ne '') {
  90. return;
  91. } else {
  92. close($self->{sock});
  93. }
  94. $self->{sock} = new
  95. IO::Socket::INET(PeerAddr => $self->{host},
  96. PeerPort => $self->{port},
  97. Proto => "tcp",
  98. Type => SOCK_STREAM)
  99. or croak "error: could not connect to $self->{host}:$self->{port}";
  100. }
  101. sub getdata {
  102. my $self = shift;
  103. my $sock;
  104. my $data;
  105. my $tries;
  106. $tries = 0;
  107. while (1) {
  108. $self->connect();
  109. $sock = $self->{sock};
  110. $data = <$sock>;
  111. if ((index($data, "\012") != -1) && (index($data, ';') != -1)) {
  112. $self->{rawdata} = $data;
  113. return $data;
  114. } else {
  115. croak "error: tried to get data $tries times and failed"
  116. if (++$tries == $self->{retry});
  117. }
  118. }
  119. }
  120. sub parse {
  121. my $self = shift;
  122. my ($stream, @streams, $name, $arg, @data, $number);
  123. $self->getdata() if ! defined $self->{rawdata};
  124. $number = 0;
  125. undef $self->{data};
  126. undef $self->{datasource};
  127. chop $self->{rawdata}; # remove ';\n'
  128. chop $self->{rawdata};
  129. @streams = split(/;/, $self->{rawdata});
  130. croak "error: expected a symux dataline with ';' delimited streams"
  131. if ($#streams < 2);
  132. $self->{datasource} = shift @streams;
  133. foreach $stream (@streams) {
  134. ($name, $arg, @data) = split(':', $stream);
  135. croak "error: expected a symux stream with ':' delimited values"
  136. if ($#data < 2);
  137. $name .= '('.$arg.')' if ($arg ne '');
  138. @{$self->{data}{$name}} = @data;
  139. $number++;
  140. }
  141. $self->{rawdata} = '';
  142. return $number;
  143. }
  144. sub getcacheditem {
  145. my ($self, $host, $streamname, $item) = @_;
  146. my ($streamtype, @addr, $element);
  147. return undef if not defined $self->{datasource};
  148. return undef if (($host ne $self->{datasource}) &&
  149. ($host ne "*"));
  150. croak "error: source $host does not contain stream $streamname"
  151. if not defined $self->{data}{$streamname};
  152. ($streamtype = $streamname) =~ s/^([a-z]+).*/\1/;
  153. if ($item eq 'timestamp') {
  154. $element = 0;
  155. } elsif (not defined $$streamitem{$streamtype}{$item}) {
  156. croak "error: unknown stream item '$item' - check symux(8) for names";
  157. } else {
  158. $element = $$streamitem{$streamtype}{$item};
  159. }
  160. return $self->{data}{$streamname}[$element];
  161. }
  162. sub getitem {
  163. my ($self, $host, $streamname, $item) = @_;
  164. my $data;
  165. my %hosts = ();
  166. undef $data;
  167. while (! defined $data) {
  168. $self->getdata();
  169. $self->parse();
  170. $hosts{$self->{datasource}} += 1;
  171. if ($hosts{$self->{datasource}} > $self->{retry}) {
  172. croak "error: seen a lot of data ($tries strings), but none that matches $host";
  173. }
  174. $data = $self->getcacheditem($host, $streamname, $item);
  175. return $data if defined $data;
  176. $tries++;
  177. }
  178. }
  179. sub source {
  180. my $self = shift;
  181. return $self->{datasource};
  182. }
  183. 1;
  184. __END__
  185. =head1 NAME
  186. SymuxClient - Object client interface for symux
  187. =head1 SYNOPSIS
  188. use SymuxClient;
  189. =head1 DESCRIPTION
  190. C<SymuxClient> provides an object client interface for listening to, and
  191. parsing of, symux data.
  192. =head1 CONSTRUCTOR
  193. =over 4
  194. =item new ( ARGS )
  195. Creates a new C<SymuxClient> object that holds both the connection to a symux
  196. and data it receives from that connection. Arguments are:
  197. host dotted quad ipv4 hostaddress
  198. port tcp port that symux is on
  199. retry* maximum number of retries; used to limit number
  200. of connection attempts and number of successive
  201. read attempts
  202. Arguments marked with * are optional.
  203. Example:
  204. $sc = new SymuxClient(host => 127.0.0.1,
  205. port => 2100);
  206. =back
  207. =head2 METHODS
  208. =item getitem (host, stream, item)
  209. Refresh the measured data and get an item from a stream for a particular
  210. host. Note that successive calls for this function deal with successive
  211. measurements of B<symon>. Set C<host> to '*' if data about any host is of
  212. interest. Any errors are sent out on STDOUT prepended with 'error: '.
  213. =item getcacheditem (host, stream, item)
  214. Get an item from a stream for a particular host. Returns C<undef> if no data is
  215. cached, or if the data cached does not match the B<host>. Can be called
  216. multiple times to obtain items from the same measurement. Set C<host> to '*' if
  217. data about any host is of interest. Any errors are sent out on STDOUT prepended
  218. with 'error: '.
  219. =item getsource ()
  220. Get the symon source host of the currently cached information. Usefull to see
  221. what host's data getcacheditem is working on.
  222. Example:
  223. $sc = new SymuxClient(host => 127.0.0.1,
  224. port => 2100);
  225. print $sc->getitem("127.0.0.1", "if(de0)",
  226. "packets_out");
  227. # get more data from that measurement
  228. print $sc->getcacheditem("127.0.0.1","if(de0)",
  229. "packets_in");
  230. # start a new measurement
  231. print $sc->getitem("*", "if(de0)",
  232. "packets_out");
  233. # which hosts packets_out was that?
  234. print $sc->getsource();
  235. =cut