Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
I'm currently experiencing difficulty with assigning the crontab to do the following: - Perl Script: - Code: #!/usr/bin/perl use strict; use warnings; #Declares the Global Variables, and Creates the file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Posts
    17

    Post Having Trouble with crontab and logrotate


    I'm currently experiencing difficulty with assigning the crontab to do the following: -
    Perl Script: -
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    #Declares the Global Variables, and Creates the file as well as appending the values once every 5 minutes.
    my($File_N,  $N_1, $N_2);
    $File_N = "local";
    open (FILE, ">>$File_N\.log") || die"\n Unable to create the file\.log";
    
    #Local Time: -
    print FILE "Local Timestamp: ";
    print FILE `date "+%a %d %b %Y %R:%S %p\n"`;
    
    $N_1   = `snmpget -c ICT338 -v2c -Ov  tcp:115.146.85.10:1161 iso.3.6.1.4.1.2021.100.4.0 | cut -b10-33`;
    $N_2   = `snmpget -c ICT338 -v2c -Ov  tcp:115.146.85.10:1161 iso.3.6.1.4.1.2021.10.1.5.2 | cut -b10-33`;
    
    print FILE"Server Name: Cloud Server\n";
    print FILE"Server Time: $N_1";
    print FILE"LOAD-5's Integer Value(s): $N_2\n";
    
    #Closes the File: - 
    close FILE;
    exit
    Output of the local.log file created by the script: -
    Code:
    Local Timestamp: Sun 19 May 2013 02:50:56 AM
    
    Server Name: Cloud Server
    Server Time: Sun May 19 06:05:49 2013
    LOAD-5's Integer Value(s): 2
    - Runs the Perl Script once every 5 minutes: -
    Code:
    crontab -e
    */1     *      *     *    *  cd /home/user/Desktop ; ./Test.pl
    - Mails the file created from the script(local.log) to the cron.daily
    (creating folders based on the local timestamp
    [date "+%a %d %b %Y %R:%S %p\n], and adding the
    local.log into the file) on a daily biases (at 8:30 AM): -
    Code:
    crontab -e
    MAILTO=root@localhost
      */2     *      *     *    *   cp /home/User/local.log /etc/cron.daily/[date "+%a %d %b %Y %R:%S %R:%S %p]/local.log
    -Finally, assigning a logrotate to keep the local.log file created from the script formatted on a daily biases as well by using logrotate(I'm not sure what to do on this part yet).

    Keep in mind that I'm using cron to run every 1-2 minutes for testing procedure.

    Post away if you know an alternative to this situation .
    Last edited by Khrz; 05-19-2013 at 12:38 PM.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    i see your code, but can you explain exactly what it is you are trying to accomplish? there may be a more straight-forward way to do what you want.

  3. #3
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by atreyu View Post
    hi,

    i see your code, but can you explain exactly what it is you are trying to accomplish? there may be a more straight-forward way to do what you want.
    I'm trying to execute the perl script which makes the local file and appends it once every 5 minutes. Email the local file to the local system administrator on daily biases at 8:30 AM. After I've sent it to the local sys admin (root@localhost), I'd have to logrotate the file to avoid it from being overwhelmed, and divide the local.log by making folders that are named under the local date format, and adding the local log for each day into the that folder (e.g. local.log have been sent to the root@localhost system. At the same time, cron gets to make a folder in the same date. And moves the file inside Today's folder).

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by atreyu View Post
    hi,

    i see your code, but can you explain exactly what it is you are trying to accomplish? there may be a more straight-forward way to do what you want.
    Also, Is there a way to filter the MIB Agent into viewing the Load Average details in a more of a specific manner using snmpd.conf as well?
    (e.g. in snmpd.conf, you'd get to filter the user from accessing a part of the tree (which is the load average): -
    snmpwalk -v2c -c ICT338 localhost iso.3.6.1.4.1.2021.10.1
    Code:
    iso.3.6.1.4.1.2021.10.1.1.1 = INTEGER: 1
    iso.3.6.1.4.1.2021.10.1.1.2 = INTEGER: 2
    iso.3.6.1.4.1.2021.10.1.1.3 = INTEGER: 3
    iso.3.6.1.4.1.2021.10.1.2.1 = STRING: "Load-1"
    iso.3.6.1.4.1.2021.10.1.2.2 = STRING: "Load-5"
    iso.3.6.1.4.1.2021.10.1.2.3 = STRING: "Load-15"
    iso.3.6.1.4.1.2021.10.1.3.1 = STRING: "0.33"
    iso.3.6.1.4.1.2021.10.1.3.2 = STRING: "0.17"
    iso.3.6.1.4.1.2021.10.1.3.3 = STRING: "0.33"
    iso.3.6.1.4.1.2021.10.1.4.1 = STRING: "12.00"
    iso.3.6.1.4.1.2021.10.1.4.2 = STRING: "12.00"
    iso.3.6.1.4.1.2021.10.1.4.3 = STRING: "12.00"
    iso.3.6.1.4.1.2021.10.1.5.1 = INTEGER: 33
    iso.3.6.1.4.1.2021.10.1.5.2 = INTEGER: 17
    iso.3.6.1.4.1.2021.10.1.5.3 = INTEGER: 33
    iso.3.6.1.4.1.2021.10.1.6.1 = Opaque: Float: 0.330000
    iso.3.6.1.4.1.2021.10.1.6.2 = Opaque: Float: 0.170000
    iso.3.6.1.4.1.2021.10.1.6.3 = Opaque: Float: 0.330000
    iso.3.6.1.4.1.2021.10.1.100.1 = INTEGER: 0
    iso.3.6.1.4.1.2021.10.1.100.2 = INTEGER: 0
    iso.3.6.1.4.1.2021.10.1.100.3 = INTEGER: 0
    iso.3.6.1.4.1.2021.10.1.101.1 = ""
    iso.3.6.1.4.1.2021.10.1.101.2 = ""
    iso.3.6.1.4.1.2021.10.1.101.3 = ""
    I only need some of the load 5 values(Just the integer, and the value name would suffice for the whole snmpd.conf) to display everytime i call in the local host using: -

    Code:
    snmpwalk -v2c -c Test localhost
    The same steps applies to the cloud server as well.

    If there is a possibility of doing so, then that would be much appreciated if You or Anyone is able to share the answer with me.

  6. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Khrz View Post
    I'm trying to execute the perl script which makes the local file and appends it once every 5 minutes.
    okay, that i get.

    Email the local file to the local system administrator on daily biases at 8:30 AM.
    1. Why do you have to email this file?
    2. Is this email being sent to root on the same system?

    After I've sent it to the local sys admin (root@localhost), I'd have to logrotate the file to avoid it from being overwhelmed
    logrotate can easily manage this for you. look at the examples in the /etc/logrotate.d directory. here's what an example might look like:

    Code:
    /path/to/local.log {
        missingok
        notifempty
        size 30k
    }
    run man logrotate to see see what these parameters mean.

    , and divide the local.log by making folders that are named under the local date format, and adding the local log for each day into the that folder (e.g. local.log have been sent to the root@localhost system. At the same time, cron gets to make a folder in the same date. And moves the file inside Today's folder).
    okay...here's where you lose me. why are you putting files in the /etc/cron.daily directory?

  7. #6
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by atreyu View Post
    okay, that i get.


    1. Why do you have to email this file?
    2. Is this email being sent to root on the same system?


    logrotate can easily manage this for you. look at the examples in the /etc/logrotate.d directory. here's what an example might look like:

    Code:
    /path/to/local.log {
        missingok
        notifempty
        size 30k
    }
    run man logrotate to see see what these parameters mean.


    okay...here's where you lose me. why are you putting files in the /etc/cron.daily directory?
    Nevermind. I've managed to fix the problem. I'm able to send the local file to the local system admin (root@localhost) on a daily biases. The file gets to be received through /var/mail. And is sent into a folder based on the local timestamp using logrotate & logrotate.conf (localTue-x-xx), and what is left from the files inside /var/mail (local.log.1) gets to be deleted. And simply erase the data inside the local file at the same day as well using /dev/null.

    But currently i am having trouble with the snmpd.conf by filtering the views for the cloud server as well as the localhost from accessing certain parts of the tree: -

    All i want is the remote and local and remote hostname, local and remote timestamp, and one of the Load-5 values (either an integer, float, or a string). That's all what i wanted to view.

    Here is what I've added in the snmpd.conf: -
    Code:
    ##### SNMP Configuration Versions 1 and 2 #####
    ##### Community String #####
    #com2sec location1 115.146.85.10:1161       Test
    #com2sec location3 localhost                Test
    ## Security Name #####
    #group LocalHostGroup1 v2c location1
    #group LocalHostGroup2 v2c location2
    ##### View of Tree #####
    #view systemview included SNMPv2-MIB::sysName.0
    #view systemview included SNMPv2-SMI::enterprises.2021.100.4.0
    #view systemview included SNMPv2-SMI::enterprises.2021.10.1.5.2
    #view systemview included SNMPv2-SMI::enterprises.2021.10.1.2.2
    #view systemview excluded .1 80
    #view systemview excluded .1
    #####
    #access LocalHostGroup1 "" any noauth exact systemview none none
    #access LocalHostGroup2 "" any noauth exact systemview none none
    Last edited by Khrz; 05-22-2013 at 05:35 AM.

  8. #7
    Just Joined!
    Join Date
    May 2013
    Posts
    17

    Post Problems with filtering views in snmpd.conf

    I am having trouble with the snmpd.conf by filtering the views for the cloud server as well as the localhost from accessing certain parts of the tree: -

    Code:
    #Local Timestamp: Mon 20 May 2013 15:33:01 PM
    #
    #Server Name: Cloud Server
    #Server Time: Mon May 20 07:32:29 2013
    #LOAD-5's Integer Value(s): 5
    #
    #Server Name: LocalHost's SNMP Server
    #Server Time: Mon May 20 15:33:01 2013
    #LOAD-5's Integer Value(s): 13
    All i want is the local and remote hostname, local and remote timestamp, and one of the Load-5 values (either an integer, float, or a string). That's all what i wanted to view.

    Here is what I've added in the snmpd.conf: -
    Code:
    ##### SNMP Configuration Versions 1 and 2 #####
    ##### Community String #####
    com2sec location1 115.146.85.10:1161  Test1
    com2sec location3 localhost                 Test
    ## Security Name #####
    group LocalHostGroup1 v2c location1
    group LocalHostGroup2 v2c location2
    ##### View of Tree #####
    view systemview included SNMPv2-SMI::enterprises.2021.10.1.2.2
    view systemview included SNMPv2-MIB::sysName.0
    view systemview included SNMPv2-SMI::enterprises.2021.10.1.2.2
    view systemview included SNMPv2-SMI::enterprises.2021.100.4.0
    view systemview included SNMPv2-SMI::enterprises.2021.10.1.5.2
    view systemview excluded .1 80
    view systemview excluded .1
    #####
    access LocalHostGroup1 "" any noauth exact systemview none none
    access LocalHostGroup2 "" any noauth exact systemview none none
    If there is any alternative to this filtering problem. Then please, post away.
    Last edited by Khrz; 05-23-2013 at 12:09 AM.

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Khrz View Post
    But currently i am having trouble with the snmpd.conf by filtering the views for the cloud server as well as the localhost from accessing certain parts of the tree: -

    All i want is the remote and local and remote hostname, local and remote timestamp, and one of the Load-5 values (either an integer, float, or a string). That's all what i wanted to view.
    can you not specify the MIB OID absolutely? e.g.:

    Code:
    snmpwalk -v2c -c ICT338 localhost iso.3.6.1.4.1.2021.10.1.2.2
    as to just getting the value, you can try the -Ov switch to snmpwalk, e.g.:

    Code:
    snmpwalk -v2c -c ICT338 -Ov localhost iso.3.6.1.4.1.2021.10.1.2.2
    check out man snmpcmd for more snmpwalk options at your disposal.

    note that you can also simply filter the output using grep, e.g.:

    Code:
    snmpwalk ...|egrep -i 'hostname|timestamp'

  10. #9
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by atreyu View Post
    can you not specify the MIB OID absolutely? e.g.:

    Code:
    snmpwalk -v2c -c ICT338 localhost iso.3.6.1.4.1.2021.10.1.2.2
    as to just getting the value, you can try the -Ov switch to snmpwalk, e.g.:

    Code:
    snmpwalk -v2c -c ICT338 -Ov localhost iso.3.6.1.4.1.2021.10.1.2.2
    check out man snmpcmd for more snmpwalk options at your disposal.

    note that you can also simply filter the output using grep, e.g.:

    Code:
    snmpwalk ...|egrep -i 'hostname|timestamp'
    I can't use snmpwalk and snmpcmd in my perl script, I'd need to use snmpget to take out the values and print it out to the local file. But what i do need is, filtering the views to show only the values that i need as well using snmpwalk.

    Here are the values that i need to filter: -
    (If i were to execute the local host using snmpwalk -c Test -v2c localhost, I'd expect to see an output such as this)
    Code:
    SNMPv2-MIB::sysName.0 = STRING: ubuntu
    SNMPv2-SMI::enterprises.2021.100.4.0 = STRING: "Wed May 22 00:32:46 2013"
    SNMPv2-SMI::enterprises.2021.10.1.2.2 = STRING: "Load-5"
    SNMPv2-SMI::enterprises.2021.10.1.5.2 = INTEGER: 3

  11. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Khrz View Post
    I can't use snmpwalk and snmpcmd in my perl script, I'd need to use snmpget to take out the values and print it out to the local file. But what i do need is, filtering the views to show only the values that i need as well using snmpwalk.
    what's the difference b/t snmpwalk and snmpget? what do you mean by "can't use smpwalk" in a perl script? do you mean it is not installed on your system, or that you don't know how to call it in a perl script and capture the output?

    i assume you mean the latter. try this approach:
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    # the snmp system command to run
    my $cmd = "snmpget blah blah";
    
    # a variable where output will be saved
    my $saved;
    
    # run the command
    open(PH,"$cmd|") or die "can't run '$cmd': $!\n";
    while(<PH>){
      chomp;
      if(/match_some_string/){
        # print matched string to terminal
        print "string '$_' matches\n";
        # save it to a variable for later use
        $saved .= $_;
      }
    }
    close(PH);
    replace the match_some_string above with the output from the snmpget command that you are interested in.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •