<commit_before>#
# Copyright (c) 2014 Aconex
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 

use strict;
use warnings;
use File::Basename;
use lib dirname (__FILE__);

use PCP::PMDA;
use LWP::UserAgent;
use RESTClient;
use ActiveMQ;
use Data::Dumper;

my $rest_hostname = 'localhost';
my $rest_port = 8161;
my $rest_username = 'admin';
my $rest_password = '<PASSWORD>';
my $rest_realm = 'ActiveMQRealm';
my $rest_timeout = 1;

my $queue_indom = 0;

# Configuration files for overriding the above settings
for my $file (pmda_config('PCP_PMDAS_DIR') . '/activemq/activemq.conf', 'activemq.conf') {
    eval `cat $file` unless ! -f $file;
}

my $http_client = LWP::UserAgent->new;
$http_client->agent('pmdaactivemq');
$http_client->timeout($rest_timeout);
my $rest_client = RESTClient->new($http_client, $rest_hostname, $rest_port, $rest_username, $rest_password, $rest_realm);
my $activemq = ActiveMQ->new($rest_client);

my %queue_instances;

my @cluster_cache;		# time of last refresh for each cluster
my $cache_interval = 2;		# min secs between refreshes for clusters

my $pmda = PCP::PMDA->new('activemq', 133);

sub update_activemq_status
{
    my ($cluster) = @_;
    my $now = time;

    if (defined($cluster_cache[$cluster]) && $now - $cluster_cache[$cluster] <= $cache_interval) {
        return;
    }

    if ($cluster == 1) {
        $activemq->refresh_health;
    }
    elsif ($cluster == 2) {
        my @queues = $activemq->queues;

        %queue_instances = map {
          ($_->short_name(), $_->short_name());
        } @queues;

        $pmda->replace_indom($queue_indom, \%queue_instances);
    }

    $cluster_cache[$cluster] = $now;
}


sub activemq_value
{
    my ( $value ) = @_;

    return (PM_ERR_APPVERSION, 0) unless (defined($value));
    return ($value, 1);
}

sub metric_subname
{
    my ($cluster, $item) = @_;

    my $metric_name = pmda_pmid_name($cluster, $item);
    my @metric_subnames = split(/\./, $metric_name);
    return $metric_subnames[-1];
}

sub activemq_fetch_callback
{
	my ($cluster, $item, $inst) = @_;

    if($cluster ==0) {
        if ($inst != PM_IN_NULL)	{ return (PM_ERR_INST, 0); }
        return activemq_value($activemq->attribute_for(metric_subname($cluster, $item)));
    }
    elsif($cluster ==1) {
        if ($inst != PM_IN_NULL)	{ return (PM_ERR_INST, 0); }
        return activemq_value($activemq->attribute_for(metric_subname($cluster, $item), 'Health'));
    }
    elsif ($cluster == 2) {
    	if ($inst == PM_IN_NULL)	{ return (PM_ERR_INST, 0); }

    	my $instance_queue_name = pmda_inst_lookup($queue_indom, $inst);
    	return (PM_ERR_INST, 0) unless defined($instance_queue_name);

	    my $selected_queue = $activemq->queue_by_short_name($instance_queue_name);
	    return (PM_ERR_INST, 0) unless defined($selected_queue);
        return activemq_value($selected_queue->attribute_for(metric_subname($cluster, $item)));
    }
    else {
        return (PM_ERR_PMID, 0);
    }
}

my %broker_metrics = (
    'total_message_count' => {
        description	=> 'Number of unacknowledged messages on the broker',
        metric_type	=> PM_SEM_COUNTER,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'average_message_size' => {
        description	=> 'Average message size on this broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'broker_id' => {
        description	=> 'Unique id of the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)}
);

my $metricCounter = 0;

foreach my $metricName (sort (keys %broker_metrics)) {
    my %metricDetails = %{$broker_metrics{$metricName}};
    $pmda->add_metric(pmda_pmid(0,$metricCounter), $metricDetails{data_type}, PM_INDOM_NULL,
        $metricDetails{metric_type}, $metricDetails{units},
        'activemq.broker.' . $metricName, $metricDetails{description}, '');
     $metricCounter++;
}
<commit_msg>Introduced a number of broker related metrics for activemq<commit_after>#
# Copyright (c) 2014 Aconex
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 

use strict;
use warnings;
use File::Basename;
use lib dirname (__FILE__);

use PCP::PMDA;
use LWP::UserAgent;
use RESTClient;
use ActiveMQ;
use Data::Dumper;

my $rest_hostname = 'localhost';
my $rest_port = 8161;
my $rest_username = 'admin';
my $rest_password = '<PASSWORD>';
my $rest_realm = 'ActiveMQRealm';
my $rest_timeout = 1;

my $queue_indom = 0;

# Configuration files for overriding the above settings
for my $file (pmda_config('PCP_PMDAS_DIR') . '/activemq/activemq.conf', 'activemq.conf') {
    eval `cat $file` unless ! -f $file;
}

my $http_client = LWP::UserAgent->new;
$http_client->agent('pmdaactivemq');
$http_client->timeout($rest_timeout);
my $rest_client = RESTClient->new($http_client, $rest_hostname, $rest_port, $rest_username, $rest_password, $rest_realm);
my $activemq = ActiveMQ->new($rest_client);

my %queue_instances;

my @cluster_cache;		# time of last refresh for each cluster
my $cache_interval = 2;		# min secs between refreshes for clusters

my $pmda = PCP::PMDA->new('activemq', 133);

sub update_activemq_status
{
    my ($cluster) = @_;
    my $now = time;

    if (defined($cluster_cache[$cluster]) && $now - $cluster_cache[$cluster] <= $cache_interval) {
        return;
    }

    if ($cluster == 1) {
        $activemq->refresh_health;
    }
    elsif ($cluster == 2) {
        my @queues = $activemq->queues;

        %queue_instances = map {
          ($_->short_name(), $_->short_name());
        } @queues;

        $pmda->replace_indom($queue_indom, \%queue_instances);
    }

    $cluster_cache[$cluster] = $now;
}


sub activemq_value
{
    my ( $value ) = @_;

    return (PM_ERR_APPVERSION, 0) unless (defined($value));
    return ($value, 1);
}

sub metric_subname
{
    my ($cluster, $item) = @_;

    my $metric_name = pmda_pmid_name($cluster, $item);
    my @metric_subnames = split(/\./, $metric_name);
    return $metric_subnames[-1];
}

sub activemq_fetch_callback
{
	my ($cluster, $item, $inst) = @_;

    if($cluster ==0) {
        if ($inst != PM_IN_NULL)	{ return (PM_ERR_INST, 0); }
        return activemq_value($activemq->attribute_for(metric_subname($cluster, $item)));
    }
    elsif($cluster ==1) {
        if ($inst != PM_IN_NULL)	{ return (PM_ERR_INST, 0); }
        return activemq_value($activemq->attribute_for(metric_subname($cluster, $item), 'Health'));
    }
    elsif ($cluster == 2) {
    	if ($inst == PM_IN_NULL)	{ return (PM_ERR_INST, 0); }

    	my $instance_queue_name = pmda_inst_lookup($queue_indom, $inst);
    	return (PM_ERR_INST, 0) unless defined($instance_queue_name);

	    my $selected_queue = $activemq->queue_by_short_name($instance_queue_name);
	    return (PM_ERR_INST, 0) unless defined($selected_queue);
        return activemq_value($selected_queue->attribute_for(metric_subname($cluster, $item)));
    }
    else {
        return (PM_ERR_PMID, 0);
    }
}

my %broker_metrics = (
    'total_message_count' => {
        description	=> 'Number of unacknowledged messages on the broker',
        metric_type	=> PM_SEM_COUNTER,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'average_message_size' => {
        description	=> 'Average message size on this broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'broker_id' => {
        description	=> 'Unique id of the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'memory_percent_usage' => {
        description	=> 'Percent of memory limit used',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U32,
        units	=> pmda_units(0,0,0,0,0,PM_COUNT_ONE)},
    'memory_limit' => {
        description	=> 'Memory limit, in bytes, used for holding undelivered messages before paging to temporary storage',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'total_connections_count' => {
        description	=> 'Total number of connections',
        metric_type	=> PM_SEM_COUNTER,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'store_limit' => {
        description	=> 'Disk limit, in bytes, used for persistent messages before producers are blocked',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'total_consumer_count' => {
        description	=> 'Number of message consumers subscribed to destinations on the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'slave' => {
        description	=> 'Slave broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'data_directory' => {
        description	=> 'The location of the data directory',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'uptime' => {
        description	=> 'Uptime of the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'temp_limit' => {
        description	=> 'Disk limit, in bytes, used for non-persistent messages and temporary data before producers are blocked',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'broker_name' => {
        description	=> 'The name of the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'job_scheduler_store_limit' => {
        description	=> 'Disk limit, in bytes, used for scheduled messages before producers are blocked',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'min_message_size' => {
        description	=> 'Min message size on this broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'job_scheduler_store_percent_usage' => {
        description	=> 'Percent of job store limit used',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U32,
        units	=> pmda_units(0,0,0,0,0,PM_COUNT_ONE)},
    'total_dequeue_count' => {
        description	=> 'Number of messages that have been acknowledged on the broker',
        metric_type	=> PM_SEM_COUNTER,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'broker_version' => {
        description	=> 'Number of messages that have been acknowledged on the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'average_message_size' => {
        description	=> 'Average message size on this broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'statistics_enabled' => {
        description	=> 'Broker statistics enabled',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
    'current_connections_count' => {
        description	=> 'Current Connections Count',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'total_producer_count' => {
        description	=> 'Number of message producers active on destinations on the broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'store_percent_usage' => {
        description	=> 'Percent of store limit used',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U32,
        units	=> pmda_units(0,0,0,0,0,PM_COUNT_ONE)},
    'max_message_size' => {
        description	=> 'Max message size on this broker',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_FLOAT,
        units	=> pmda_units(1,0,0,PM_SPACE_BYTE,0,0)},
    'total_enqueue_count' => {
        description	=> 'Number of messages that have been sent to the broker',
        metric_type	=> PM_SEM_COUNTER,
        data_type	=> PM_TYPE_U64,
        units	=> pmda_units(0,0,1,0,0,PM_COUNT_ONE)},
    'temp_percent_usage' => {
        description	=> 'Percent of temp limit used',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_U32,
        units	=> pmda_units(0,0,0,0,0,PM_COUNT_ONE)},
    'persistent' => {
        description	=> 'Messages are synchronized to disk',
        metric_type	=> PM_SEM_INSTANT,
        data_type	=> PM_TYPE_STRING,
        units	=> pmda_units(0,0,0,0,0,0)},
);

my $metricCounter = 0;

foreach my $metricName (sort (keys %broker_metrics)) {
    my %metricDetails = %{$broker_metrics{$metricName}};
    $pmda->add_metric(pmda_pmid(0,$metricCounter), $metricDetails{data_type}, PM_INDOM_NULL,
        $metricDetails{metric_type}, $metricDetails{units},
        'activemq.broker.' . $metricName, $metricDetails{description}, '');
     $metricCounter++;
}

{'fraction_non_alphanumeric': 0.13121575698730842, 'fraction_numerical': 0.017130175462930854, 'mean_word_length': 3.4643962848297214, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 187, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4104422', 'n_tokens_mistral': 5057, 'n_tokens_neox': 4921, 'n_words': 1114}