Code Monkey home page Code Monkey logo

collectd-plugins-redisclient's Introduction

package Collectd::Plugins::RedisClient;

use strict;
use warnings;
use Collectd qw( :all );

use Redis;

# ABSTRACT: collectd plugin for reading counters from a redis server
# VERSION

=head1 SYNOPSIS

This is a collectd plugin for reading counters from a redis server.

In your collectd config:

    <LoadPlugin "perl">
    	Globals true
    </LoadPlugin>

    <Plugin "perl">
      BaseName "Collectd::Plugins"
      LoadPlugin "RedisClient"

    	<Plugin "RedisClient">
		prefix "redis"
                metric "sa-timing.*.count" "counter"
                metric "sa-timing.*.time" "counter"
    	</Plugin>
    </Plugin>

=head1 SEE ALSO

L<Mail::SpamAssassin::Plugin::RuleTimingRedis>

=cut

our $prefix = 'redis';
our $server = '127.0.0.1:6379';
our $metrics = { };

our $redis;

sub redis_client_stats_config {
    my ($ci) = @_;
    foreach my $item (@{$ci->{'children'}}) {
        my $key = lc($item->{'key'});
        my $val = $item->{'values'};

        if ($key eq 'server' ) {
            $server = $val->[0];
        } elsif ($key eq 'prefix' ) {
            $prefix = $val->[0];
        } elsif ($key eq 'metric') {
            $metrics->{$val->[0]} = $val->[1];
        }
    }
    return 1;
}

sub _connect_redis {
  if( ! defined $redis ) {
    $redis = Redis->new( 'server' => $server );
  }
  return $redis;
}

sub redis_client_stats_read {
    eval { _connect_redis(); };
    if( $@ ) {
      plugin_log(LOG_WARNING, "cant connect to redis: $@");
      return 1;
    }
    
    foreach my $metric (keys %$metrics) {
        my $type  = $metrics->{$metric};

        my @keys = $redis->keys( $metric );
        if( scalar(@keys) == 0 ) { next; }

        my @values = $redis->mget( @keys );
        for( my $i = 0 ; $i < scalar @keys ; $i++ ) {
            my $vl = {
                plugin => 'redis_client',
                plugin_instance => $prefix,
                type => $type,
                type_instance => $keys[$i],
                values => [ $values[$i] ],
            };
            plugin_dispatch_values($vl);
        }
    }
    return 1;
}

plugin_register(TYPE_CONFIG, "RedisClient", "redis_client_stats_config");
plugin_register(TYPE_READ, "RedisClient", "redis_client_stats_read");

1;

collectd-plugins-redisclient's People

Contributors

benningm avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.