Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic probe running an arbitrary command. #415

Open
shamefulCake1 opened this issue Aug 27, 2024 · 1 comment
Open

Generic probe running an arbitrary command. #415

shamefulCake1 opened this issue Aug 27, 2024 · 1 comment

Comments

@shamefulCake1
Copy link

shamefulCake1 commented Aug 27, 2024

I would like to ask that a "GenericProbe" which allows just writing a command into "config" be added to Smokeping.

I have actually written a similar probe for myself, but my Perl skills are sub-standard, so the code quality might not match what is expected of a mature software.

Nevertheless, here is the code:

package Smokeping::probes::GenericCommand;

=head1 301 Moved Permanently

This is a Smokeping probe module. Please use the command 

C<smokeping -man Smokeping::probes::GenericCommand>

to view the documentation or the command

C<smokeping -makepod Smokeping::probes::GenericCommand>

to generate the POD document.

=cut

use strict;
use base qw(Smokeping::probes::basefork);
use IPC::Open3;
use Symbol;
use Carp;
use Time::HiRes qw(gettimeofday tv_interval);

sub pod_hash {
	return {
		name => <<DOC,
Smokeping::probes::GenericCommand - Generic Command Probe for SmokePing
DOC
		description => <<DOC,
Runs a command given as a parameter n times, and records stdout (should be a number)
If you are using this module to monitor hdd temperature, you might need
setcap 'CAP_SYS_RAWIO+eip CAP_DAC_OVERRIDE+eip CAP_SYS_ADMIN+eip' /usr/sbin/smartctl
DOC
		authors => <<'DOC',
Anon 
DOC
	}
}

sub new($$$)
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = $class->SUPER::new(@_);

    # no need for this if we run as a cgi

    return $self;
}

sub ProbeDesc($){
    my $self = shift;
    return "Generic Command";
}

sub pingone ($){
    my $self = shift;
    my $target = shift;

    my $inh = gensym; 
    my $outh = gensym;
    my $errh = gensym;

    my $host = $target->{addr};

    #my $query = "$self->{properties}{binary} -t $target->{vars}->{keytype}  -p $target->{vars}->{port} $host";
    #my $query = q(timeout 10 /usr/sbin/smartctl -x /dev/nvme0 | grep -F 'Temperature:' | awk '{printf($2);}' || echo 9999);
    my $query = "$target->{vars}->{command}";
    my @times;
    my @values;

    # get the user and system times before and after the test
    $self->do_debug("query=$query\n");
    for (my $run = 0; $run < $self->pings; $run++) {
       my $t0 = [gettimeofday()];

       my $pid = open3($inh,$outh,$errh, $query);
       while (my $line = <$outh>) {
           my $num;
           chomp $line;
           $num = int($line);
           push (@values, $num);
           push @times, tv_interval($t0);
        }
	waitpid $pid,0;
	my $rc = $?;
	carp "$query returned with exit code $rc. run with debug enabled to get more information" unless $rc == 0;
	close $errh;
	close $inh;
	close $outh;

    }
    @times =  map {sprintf "%.10e", $_ } sort {$a <=> $b} @times;
    @values =  map {sprintf "%.10e", $_ } sort {$a <=> $b} @values;

    $self->do_debug("time=@times\n");
    return @values;
    return @times;
}

sub probevars {
	my $class = shift;
	return $class->_makevars($class->SUPER::probevars, {})
}

sub targetvars {
        my $class = shift;
        return $class->_makevars($class->SUPER::targetvars, {
            _mandatory => [ 'command' ],
           command => {
               _doc => "command to run",
	       _re => '.+',
               _example => q(/usr/sbin/smartctl -x /dev/nvme0 | grep -F 'Temperature:' | awk '{printf($2);}'),
           },
       })
}
1;
@github-staff github-staff deleted a comment from mayank785 Oct 23, 2024
@github-staff github-staff deleted a comment from mayank785 Oct 23, 2024
Copy link

This issue has become stale and will be closed automatically within 7 days. Comment on the issue to keep it alive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@shamefulCake1 and others