Discovery: How to run a query on a regular schedule (using crontab) and send the output via email or to an SMB share?

Version 1
    Share This:

    This document contains official content from the BMC Software Knowledge Base. It is automatically updated when the knowledge article is modified.


    PRODUCT:

    BMC Discovery


    COMPONENT:

    BMC Discovery 11.3


    APPLIES TO:

    BMC Discovery



    PROBLEM:

    How to run a query on a regular schedule (using crontab) and send the output via email or to an SMB share?


    SOLUTION:

    Discovery does not currently have a facility to do this. However, it is possible to run a query on a regular basis by using tw_query and scheduling it in a cron job. See:
    https://docs.bmc.com/docs/discovery/113/tw_query-788110376.html
    https://docs.bmc.com/docs/discovery/113/tw_cron_update-788110359.html

       
    • Following is an example of a script that can schedule a query and send the result by email.  The dos2unix command is needed to prevent blank lines in the output Excel spreadsheet.
      
      # ==================  /usr/tideway/check_on_consolidation.sh ====================  
    ### First, remove previous output file, if it is there  
    rm -f /tmp/results.csv  
     
    ### Run the query  
    /usr/tideway/bin/tw_query --username=reports --passwordfile=pwd  --csv --file /tmp/results.csv  "SEARCH IN '_System' ScanningSystem SHOW name, formatTime(last_data_push)"  
        
    ### Remove the additional blank lines from the output csv file  
    dos2unix /tmp/results.csv  
       
    ### Email the results. Provide the relevant smtp info and email address!!  
    echo "my query results" | smtp=mailhost.bmc.com mailx -a "/tmp/results.csv" -n -s "daily report from consolidator"   myname@bmc.com  
    ================== 
       
       
    • Following is an example of an alternate script that can schedule a query and copy the result to an SMB share.  The dos2unix command is needed to prevent blank lines in the output Excel spreadsheet.
      
      # ==================  /usr/tideway/check_on_consolidation.sh ====================  
    ### First, remove previous output file, if it is there  
    rm -f /tmp/results.csv  
     
    ### Run the query  
    /usr/tideway/bin/tw_query --username=reports --passwordfile=pwd  --csv --file /tmp/results.csv  "SEARCH IN '_System' ScanningSystem SHOW name, formatTime(last_data_push)"  
        
    ### Remove the additional blank lines from the output csv file  
    dos2unix /tmp/results.csv  
       
    ### Copy the results to an SMB share. Provide the relevant SMB info!!  
    smbclient //   server-name/   share -c 'put /tmp/results.csv results.csv' -U '   domain-name\   username%   password'  
    ================== 
      
    In the scripts above, notice the use of --passwordfile and --username.  This is needed because the scheduled job can't prompt for a password. 
    In the above examples, there is a user account named 'reports',   which only has the 'reports' permission.  The password for this user is in the file "/usr/tideway/pwd". 

    Please note that the above scripts are provided only as examples. This is considered to be a user customization and the user is responsible for creating and troubleshooting the script. 

    ===============================================
    Following are the steps to schedule this in the crontab. 

    1) Login to the appliance command line as the tideway user 

    2) Create the shell script in /usr/tideway using the example contents above. Save it as a .sh file (i.e. /usr/tideway/check_on_consolidation.sh). 

    3) Create the password file (i.e. /usr/tideway/pwd). It should contain the password for the user specified in the query. 

    4)  T  est / modify the shell script until the expected results are received: 

    /bin/sh /usr/tideway/check_on_consolidation.sh

      
    5) Create a cron job file on the appliance, for example:

    #===========/usr/tideway/etc/cron/check_on_consolidation.cron ========
    #
    # cron job to check on the consolildator

    # runs every day at 02:00
    #
    0 2 * * * /bin/sh  /usr/tideway/check_on_consolidation.sh

    #===============================================================
      
    6) Run /usr/tideway/bin/tw_cron_update to add this job to the crontab.
      
    7) Run "crontab -l" to verify that the job is on the crontab.
      

     


    Article Number:

    000298122


    Article Type:

    Solutions to a Product Problem



      Looking for additional information?    Search BMC Support  or  Browse Knowledge Articles