Permalink
Browse files

Reorganize start_elem for efficiency, better socket handling

  • Loading branch information...
dinomite committed Oct 25, 2011
1 parent 05a1e05 commit 62dced8bd7070e7f7a430392d5c3be8d0c870fdd
Showing with 28 additions and 15 deletions.
  1. +18 −8 gang2graph/Ganglia/GraphiteSender.pm
  2. +10 −7 gang2graph/gang2graph.pl
@@ -40,10 +40,8 @@ sub start_element {
# Move along if we're skipping this host
return if ($self->{skipHost} == 1);
- if ($data->{LocalName} eq 'GRID') {
- $self->_handleGrid($data);
- } elsif ($data->{LocalName} eq 'CLUSTER') {
- $self->_handleCluster($data);
+ if ($data->{LocalName} eq 'METRIC') {
+ $self->_handleMetric($data);
} elsif ($data->{LocalName} eq 'HOST') {
# Skip hosts that aren't in the list of hosts to proces
unless (grep {$data->{Attributes}->{'{}NAME'}->{Value} =~ /$_/} @{$self->{hosts}}) {
@@ -52,16 +50,25 @@ sub start_element {
}
$self->_handleHost($data);
- } elsif ($data->{LocalName} eq 'METRIC') {
- $self->_handleMetric($data);
+ } elsif ($data->{LocalName} eq 'GRID') {
+ $self->_handleGrid($data);
+ } elsif ($data->{LocalName} eq 'CLUSTER') {
+ $self->_handleCluster($data);
}
}
sub end_element {
my ($self, $data) = @_;
- # Reset the skipHost flag
- $self->{skipHost} = 0 if ($data->{LocalName} eq 'HOST');
+ if ($data->{LocalName} eq 'METRIC') {
+ } elsif ($data->{LocalName} eq 'HOST') {
+ # Reset the skipHost flag
+ $self->{skipHost} = 0 if ($data->{LocalName} eq 'HOST');
+ } elsif ($data->{LocalName} eq 'GRID') {
+ #print "Ending grid " . $self->{grid} . "\n";
+ } elsif ($data->{LocalName} eq 'CLUSTER') {
+ #print "Ending cluster " . $self->{cluster} . "\n";
+ }
}
sub end_document {
@@ -73,16 +80,19 @@ sub end_document {
sub _handleGrid {
my ($self, $data) = @_;
$self->{grid} = $data->{Attributes}->{'{}NAME'}->{Value};
+ #print "Starting grid " . $self->{grid} . "\n";
}
sub _handleCluster {
my ($self, $data) = @_;
$self->{cluster} = $data->{Attributes}->{'{}NAME'}->{Value};
+ #print "Starting cluster " . $self->{cluster} . "\n";
}
sub _handleHost {
my ($self, $data) = @_;
$self->{host} = $data->{Attributes}->{'{}NAME'}->{Value};
+ #print "Host " . $self->{host} . "\n";
}
sub _handleMetric {
View
@@ -19,6 +19,7 @@ package App::Gang2Graph;
my $xml = getXMLFromSocket();
+# Socket for sending to Graphite
my $graphiteSocket = IO::Socket::INET->new(
PeerHost => $graphiteHost,
PeerPort => $graphitePort,
@@ -31,23 +32,25 @@ package App::Gang2Graph;
),
);
$parser->parse_string($xml);
+$graphiteSocket->close();
sub getXMLFromSocket {
my $socket = IO::Socket::INET->new(
PeerHost => $gangliaHost,
PeerPort => $gangliaPort,
) or die "Couldn't connect to " . $gangliaHost . ':' . $gangliaPort . " - $!\n";
- my $xml;
- my $part;
- my $recvSize = 1048576;
+ my $xml = '';
+ my $part = '';
+ my $recvSize = 1024;
$socket->recv($part, $recvSize);
- my $limit = 100000;
- while ($part ne '' && $limit > 0) {
- $socket->recv($part, $recvSize);
+ # Accumulate the XML file from the socket
+ my $chunk = 0;
+ while ($part ne '' && $chunk < 1_000_000) {
$xml .= $part;
- $limit--;
+ $socket->recv($part, $recvSize);
+ $chunk++
}
return $xml;

0 comments on commit 62dced8

Please sign in to comment.