SymuxClient.pm 8.9 KB

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