<commit_before>#
# Copyright (c) 2015 <NAME>.
# All rights reserved.
#
# 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 PCP::PMDA;
use Slurm;
use Sys::Hostname;

use vars qw( $pmda $slurm );


# indoms
my ( $slurm_indom, $node_job_indom, $job_indom, $user_indom, $node_indom ) = ( 0, 1, 2, 3, 4);

my $GEN_CLUSTER = 0;
my $NODE_JOB_CLUSTER = 1;
my $GLOBAL_JOB_CLUSTER = 2;
my $NODE_CLUSTER = 3;
my $USER_CLUSTER = 4;
my $PARTITION_CLUSTER = 5;

# hashes for the different indoms

our %jobs = ();
our %nodejobs = ();
our %users = ();
our %nodes = ();
our $numnodes = 0;

# Who am i. To check for jobs in this host
our $host = hostname;

# Array ref for all jobs
our $all_jobs_ref;

sub slurm_init {
   $slurm = Slurm::new();
}

# Update General Stuff
sub slurm_update_cluster_gen {

    my $nodemsg = $slurm->load_node();

    unless($nodemsg) {
        die "failed to load node info: " . $slurm->strerror();
    }

    my @nodes = @{ $nodemsg->{node_array} };

    $numnodes = @nodes;

}

sub slurm_update_jobs {

    # TODO: use the time so we only update on changes
    my $jobmsg = $slurm->load_jobs(0, 0);

    $all_jobs_ref = \@{ $jobmsg->{job_array} };

    %jobs = ();
    
    for my $job ( @$all_jobs_ref ){
        my $jid = $job->{job_id};
        $jobs{$jid} = $job;
    }
}

# Update jobs specific to this node
sub slurm_update_cluster_node_job {

    # Populate the all jobs reference
    &slurm_update_jobs;

    %nodejobs = ();

    # Find all jobs where I am a member
    for my $job ( @$all_jobs_ref ){
        my $nodelist = $job->{nodes};
        my $jid = $job->{job_id};
    
        if ( !(defined $nodelist and length $nodelist) ){
            # We only care about jobs that are running
            next;
        }
    
        my $hostlist = Slurm::Hostlist::create( $nodelist );
        my $num_hosts = $hostlist->count();
    
        if ( $num_hosts > 0){
            my $mypos = $hostlist->find("k05n28");
            #my $mypos = $hostlist->find($host);
            if( $mypos >= 0){
                # We only want jobs that are on this host
                $nodejobs{$jid} = $job;
            }
        }
    }
}

#
# fetch is called once by pcp for each refresh and then the fetch callback is
# called to query each statistic individually
#
sub slurm_fetch {

        # Make these dependent on what we want?

	slurm_update_cluster_gen();
        slurm_update_cluster_node_job();

	#$pmda->replace_indom($node_job_indom, \%nodejobs);<commit_msg>pmdaslurm - fix hostname lookup<commit_after>#
# Copyright (c) 2015 <NAME>.
# All rights reserved.
#
# 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 PCP::PMDA;
use Slurm;
use Net::Domain;

use vars qw( $pmda $slurm );


# indoms
my ( $slurm_indom, $node_job_indom, $job_indom, $user_indom, $node_indom ) = ( 0, 1, 2, 3, 4);

my $GEN_CLUSTER = 0;
my $NODE_JOB_CLUSTER = 1;
my $GLOBAL_JOB_CLUSTER = 2;
my $NODE_CLUSTER = 3;
my $USER_CLUSTER = 4;
my $PARTITION_CLUSTER = 5;

# hashes for the different indoms

our %jobs = ();
our %nodejobs = ();
our %users = ();
our %nodes = ();
our $numnodes = 0;

# Who am i. To check for jobs in this host
our $host = hostname;

# Array ref for all jobs
our $all_jobs_ref;

sub slurm_init {
   $slurm = Slurm::new();
}

# Update General Stuff
sub slurm_update_cluster_gen {

    my $nodemsg = $slurm->load_node();

    unless($nodemsg) {
        die "failed to load node info: " . $slurm->strerror();
    }

    my @nodes = @{ $nodemsg->{node_array} };

    $numnodes = @nodes;

}

sub slurm_update_jobs {

    # TODO: use the time so we only update on changes
    my $jobmsg = $slurm->load_jobs(0, 0);

    $all_jobs_ref = \@{ $jobmsg->{job_array} };

    %jobs = ();
    
    for my $job ( @$all_jobs_ref ){
        my $jid = $job->{job_id};
        $jobs{$jid} = $job;
    }
}

# Update jobs specific to this node
sub slurm_update_cluster_node_job {

    # Populate the all jobs reference
    &slurm_update_jobs;

    %nodejobs = ();

    # Find all jobs where I am a member
    for my $job ( @$all_jobs_ref ){
        my $nodelist = $job->{nodes};
        my $jid = $job->{job_id};
    
        if ( !(defined $nodelist and length $nodelist) ){
            # We only care about jobs that are running
            next;
        }
    
        my $hostlist = Slurm::Hostlist::create( $nodelist );
        my $num_hosts = $hostlist->count();
    
        if ( $num_hosts > 0){
            #my $mypos = $hostlist->find("k05n28");
            my $mypos = $hostlist->find($host);
            if( $mypos >= 0){
                # We only want jobs that are on this host
                $nodejobs{$jid} = $job;
            }
        }
    }
}

#
# fetch is called once by pcp for each refresh and then the fetch callback is
# called to query each statistic individually
#
sub slurm_fetch {

        # Make these dependent on what we want?

	slurm_update_cluster_gen();
        slurm_update_cluster_node_job();

	#$pmda->replace_indom($node_job_indom, \%nodejobs);
{'fraction_non_alphanumeric': 0.11803444782168186, 'fraction_numerical': 0.008443093549476529, 'mean_word_length': 2.753485424588086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7561754', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2032, 'n_words': 668}