BNMR: Shift Monitor: Difference between revisions

From DaqWiki
Jump to navigation Jump to search
en>Suz
mNo edit summary
en>Suz
mNo edit summary
Line 11: Line 11:


== Introduction ==
== Introduction ==
The BNMR shift monitor system consists of two perlscripts in directory  {{Filepath|path=/home/bnmr/online/perl}} :
The BNMR shift monitor system consists of the perlscript {{File|name=monitor.pl}} in directory  {{Filepath|path=/home/bnmr/online/perl}}, which is
<ul>
run as a '''cron job''' during the beam periodIt run as user ''bnmr'', and accesses the required information from both the {{bnmqr|join=and}} ODBs.
<li>    {{File|name=monitor.pl}}
 
<li>    {{File|name=cycle_monitor_logfiles.pl}}
The Shift Monitor program  {{File|name=monitor.pl}} keeps a running total of how much usable beam time  (in minutes) each TRIUMF experimental group has had access to, while running as experiment {{bnmqr|join=or}} (or both).  It reads the proton current from EPICS once per minute.
</ul>
The information is stored in shift files in the directory  {{Filepath|path=/isdaq/data1/bnmr/mon/<year>/}}.  
both of which are normally run as cron jobsThey are run as user ''bnmr'', and access the required information from both the {{bnmqr|join=and}} ODBs.


Output from the perlscript is sent to a [[#Log files|logfile]]. 


The Shift Monitor program  {{File|name=monitor.pl}} keeps a running total of how much usable beam time  (in minutes) each TRIUMF experimental group has had access to, while running as experiment {{bnmqr|join=or}} (or both).  It reads the proton current from EPICS once per minute.
The information is stored in shift files in the directory  {{Filepath|path=/isdaq/data1/bnmr/mon/<year>/}}. This is also the location of the log files used to store messages.
== How it works ==
== How it works ==


Line 49: Line 46:
  </li>
  </li>
</ul>
</ul>
<li> It reads the [[#How it works|experiment number]] from the enabled experiment(s).  The experiment number must be > 0, otherwise no beam time will be allocated.</li>
<li> It reads the [[#How it works|experiment number]] from the enabled experiment(s).  The experiment number must be valid (0 < expt_num < 9999), otherwise no beam time will be allocated and an error will be reported </li>
<li> The program determines the name of the shift file by date and by 12-hour shift (AM/PM).</li>
<li> The program determines the name of the shift file by date and by 12-hour shift (AM/PM).</li>
<li> If no file of that filename exists, it is created and a line written for each different experiment number, and each running experiment assigned 1 minute of running time.  </li>
<li> If no file of that filename exists, it is created and a line written for each different experiment number, and each running experiment assigned 1 minute of running time.  </li>
<li> If {{bnmqr|join=and}} are '''both''' running with the '''same''' experiment number, only one line will be created with {{bnmqr|join=and}} experiments each given 1 minute accumulated time. </li>
<li> If {{bnmqr|join=and}} are '''both''' running with the '''same''' experiment number, only one line will be created with {{bnmqr|join=and}} experiments each given 1 minute accumulated time. </li>
<li> If there is an existing file, the program looks for a line with the experiment number. If it finds one, the accumulated time is incremented for the appropriate experiment.  All other lines are copied and an updated file is written. If no line exists for this experiment number, a new line will be written.</li>
<li> If there is an existing file, the program looks for a line with the experiment number. If it finds one, the accumulated time is incremented for the appropriate experiment.  All other lines are copied and an updated file is written. If no line exists for this experiment number, a new line will be written.</li>
<li> If '''test mode''' is enabled (see [[#Input Parameters]]) an entry to the shift file written in test mode will be followed by an asterisk (see example).</li>
<li> If '''test mode''' is enabled (see [[#Input Parameters]]) an entry to the shift file written in test mode will be followed by an asterisk (see [[#Shift files]]) -- unless parameter ''skip file'' is true</li>
</ul>
</ul>


The script  {{File|name=cycle_monitor_logfiles.pl}} cycles the logfiles to save disk space. After 5 days, the logfile information is deleted.
 
;Note : the shift files are never deleted unless by the user.




== Log files ==
== Log files ==
The current log file is {{Filepath|path=/isdaq/data1/bnmr/mon/<year>/monitor.log}}
The current log file is {{Filepath|path=/isdaq/data1/bnmr/mon/monitor.log}}


When  {{File|name=running monitor.pl}} as a cron job,  the output of the program can only be found in the log file, since printing to the screen must be suppressed (to prevent excessive email).  
When  {{File|name=monitor.pl}} is running as a cron job,  the output of the program can only be found in the log file, since printing informational messages to the screen must be suppressed (any messages printed are sent as email (see MAILTO in crontab)).


== Cycling the log files ==
Logrotate (see [[#Rotate log files]]) is used to rotate the log files on a daily basis. 7 days worth of logfiles are retained.
A second cron job  {{File|name=cycle_monitor_logfiles.pl}} cycles the log files, so that a new  {{File|name=monitor.log}} file is started every 24 hours. Five days worth of logs are retained in files  {{File|name=monitor.log_1}} to  {{File|name=monitor.log_5}} where  {{File|name=monitor.log_5}} is the oldest. Each day the cron job deletes  {{File|name=monitor.log_5}}, renames  {{File|name=monitor.log_n}} to  {{File|name=monitor.log_n+1}} and renames  {{File|name=monitor.log}} to  {{File|name=monitor.log_1}}. This is done to limit the disk space taken up by the log files.  


{{File|name=cycle_monitor_logfiles.pl}} takes one parameter, which when set to 1  suppresses printing to the screen.  Output from  {{File|name=cycle_monitor_logfiles.pl}}  can be found in  {{File|name=cycle_files.log}}. A version of this file is created each time the script runs.
Note : the shift files are never rotated or deleted unless by the user.


== Shift files ==
== Shift files ==
Line 75: Line 70:
{{File|name=24Jul2014_AM.txt, 24Jul2014_PM.txt}}.  The AM shift starts at 8.30am, the PM shift at 8:30pm.
{{File|name=24Jul2014_AM.txt, 24Jul2014_PM.txt}}.  The AM shift starts at 8.30am, the PM shift at 8:30pm.


The format of the shift files is e.g.
The format of the shift files is shown in the following examples:


   
=== Example 1 ===
This example shows one experimenter group (expt number 1604) has run on  {{bnmr}} and two (1539,1419) on  {{bnqr}} during an 8-hour shift.
<pre>
# Shift Monitor File first opened at Sun Jun 12 20:30:01 2016
# Threshold(uA)  ExptNum        BNMR(min)    BNQR(min)  Time line written        Testing
  35.000        00001604      0643        0000      Mon Jun 13 08:28:01 2016   
  35.000        00001539      0000        0536      Mon Jun 13 06:32:01 2016   
  35.000        00001419      0000        0117      Mon Jun 13 08:29:01 2016   
</pre>
 
=== Example 2 Test Mode ===
In this example  {{File|name=monitor.pl}} has been run in test mode (the '''*''' under "Testing"  indicates '''test mode''' - see [[#How it works]]).
In this case, the same experiment numbers have been used for testing {{File|name=monitor.pl}} on both  {{bnmqr|join=and}}.
<pre>
  # Shift Monitor File first opened at Thu Jul 24 13:52:21 2014
  # Shift Monitor File first opened at Thu Jul 24 13:52:21 2014
  # Threshold(uA)  ExptNum        BNMR(min)      BNQR(min)  Time line written        Testing
  # Threshold(uA)  ExptNum        BNMR(min)      BNQR(min)  Time line written        Testing
   10.000        00001508        0080            0036        Thu Jul 24 15:50:01 2014    '''*'''
   10.000        00001508        0080            0036        Thu Jul 24 15:50:01 2014    *
   10.000        00001412        0035            0021        Thu Jul 24 15:49:02 2014    '''*'''
   10.000        00001412        0035            0021        Thu Jul 24 15:49:02 2014    *
 
</pre>
The '''*''' under "Testing"  indicates '''test mode''' (see [[#How it works]]).




Line 89: Line 96:
{|  style="text-align: left; width: 100%; background-color: rgb(255, 255, 255);" border="3" cellpadding="2" cellspacing="2"
{|  style="text-align: left; width: 100%; background-color: rgb(255, 255, 255);" border="3" cellpadding="2" cellspacing="2"
|+ Table 1 :  Input Parameters to <span style="color:#b3b3ff; font-style:italic;">monitor.pl</span>
|+ Table 1 :  Input Parameters to <span style="color:#b3b3ff; font-style:italic;">monitor.pl</span>
|- style="vertical-align: top; background-color: silver; "
|- style="vertical-align: top; background-color: silver; font-weight:bold; "
! colspan=2  |Input Parameter!! Explanation
! colspan=2  |Input Parameter!! Explanation
|-
|- style="vertical-align: top; background-color: whitesmoke; font-weight:bold; "
|1|| Proton Current Threshold (uA)|| User selected value. If proton current is less than this value, program will exit without updating shift file.
|1||Proton Current Threshold (uA)|| User selected value. If proton current is less than this value, program will exit without updating shift file.<br><span style="color:grey; font-weight:normal;  font-size:90%"> In Test Mode, set value to 0 to skip reading proton current from EPICS.</span>
|-  style="vertical-align: top; background-color: whitesmoke; "
|-  style="vertical-align: top; background-color: whitesmoke; font-weight:bold; "
|2|| Suppress printing || Must be TRUE if running as a cron job
|2|| Suppress printing || Must be TRUE if running as a cron job
|-
|- style="vertical-align: top;color:grey; font-size:90%"
|3||Test Mode ||Set TRUE for testing if EPICS switches are set to "off"
|3||Test Mode ||Set Test Mode TRUE for testing if EPICS switches are set to "off"
|-
|- style="vertical-align: top;color:grey; font-size:90%"
|4||BNMR enabled||In Test Mode, set TRUE to enable BNMR regardless of EPICS switch
|4||BNMR enabled||In Test Mode, set TRUE to enable BNMR regardless of EPICS switch
|-
|- style="vertical-align: top;color:grey; font-size:90%"
|5||BNQR enabled||In Test Mode, set TRUE to enable BNQR regardless of EPICS switch
|5||BNQR enabled||In Test Mode, set TRUE to enable BNQR regardless of EPICS switch
|-  style="vertical-align: top;color:grey; font-size:90%"
|6||skip file||In Test Mode, set TRUE to prevent any update of shift files
|}
|}
<br>
;NOTE:
: If not running in Test Mode, only the first two parameters are required.


== Running the program by hand ==
== Running the program by hand ==
The perlscript  {{File|name=monitor.pl}} can be run by hand to check its operation. However, each time it is run, a minute will be added to the current experiment(s) in the shift files.  
The perlscript  {{File|name=monitor.pl}} can be run by hand to check its operation. However, each time it is run, a minute will be added to the current experiment(s) in the shift files. Therefore, it should not be run by hand during the beam period (i.e. when the cron job is running) unless in test mode with the  [[#Input Parameters|input parameter]]) '''skip file'''  set true, to prevent any update of shift files.
;Therefore, it should not be run by hand during the beam period if the cron job is running.


To run the program, as user ''bnmr'', in an xterm type  e.g.
To run the program, as user ''bnmr'', in an xterm type  e.g.
  /home/bnmr/vmic_online/perl/monitor.pl  15   
  /home/bnmr/vmic_online/perl/monitor.pl  15   
Printing to the screen ( [[#Input Parameters|input parameter]] 2)  need not be suppressed when running by hand.  
Printing to the screen ( [[#Input Parameters|input parameter]] '''suppress printing''')  need not be set true when running by hand.  


The program can also be run in '''test mode''', e.g
The program can also be run in '''test mode''', e.g
  /home/bnmr/vmic_online/perl/monitor.pl  0 0 1 0 1   
  /home/bnmr/vmic_online/perl/monitor.pl  5 0 1 0 1   
This would enable the {{bnqr}} experiment regardless of the state of the proton current and the EPICS switches (see [[#Input Parameters]]).
This would enable the {{bnqr}} experiment regardless of the EPICS switches (see [[#Input Parameters]]). If the proton current is less than the threshold, in test mode the script continues after writing a message to the log file. The shift file would be updated.
 
or it can be run in '''test mode''' with [[#Input Parameters|input parameter]]) '''skip file'''  set true, to prevent any update of shift files:
/home/bnmr/vmic_online/perl/monitor.pl  0 0 1 1 1 1 
This would enable both experiments regardless of the EPICS switches. The proton current would not be read (see [[#Input Parameters]]), the shift files remain unchanged.


The program {{File|name=cycle_monitor_logfiles.pl}} can also be run by hand at any time. Printing messages to the screen will not be suppressed if run as follows:
/home/bnmr/vmic_online/perl/cycle_monitor_logfiles.pl


== Running the cron job ==
== Running the cron job ==
Line 125: Line 138:


The crontab file will be displayed as follows:  
The crontab file will be displayed as follows:  
 
<pre>
# MAILTO=GM <gmorris@triumf.ca>
# MAILTO=Gerald Morris <gmorris@triumf.ca>,suz@triumf.ca
SHELL=/bin/csh
SHELL=/bin/csh
# cron parameters :
# cron parameters :
# minute hour dayofmonth month dayofweek command
# minute hour dayofmonth month dayofweek command
#
#
  # cycle_monitor_logfiles.pl : cycle the logfiles monitor.log
# Aug 31 2016 SD use logrotate
#
# logrotate now cycles the logfiles monitor.log.  
#: output from this script in cycle_files.log
# See /etc/logrotate.d/musrmidas
# run like this as cron job (argument=1 suppresses printing to screen)
#
30 8,20 * * * /home/bnmr/vmic_online/perl/cycle_monitor_logfiles.pl 1
# monitor.pl parameters: proton_current(uA) noprint testing bnmr_running bnqr_running skip_file
#
#
#
# run like this for TESTING cron job when epics switches are not set correctly and proton I is zero:
# monitor.pl parameters: proton_current(uA) noprint testing bnmr_running bnqr_running
#
#
#use the odb to change the experiment numbers under "/experiment/edit on start/"
# run like this for TESTING cron job when epics switches are not set correctly and proton I is zero:
#* * * * * /home/bnmr/online/perl/monitor.pl 0 1 1 1 1 0
#
#
#use the odb to change the experiment numbers under "/experiment/edit on start/"
# Note:  emacs Cntrl X Cntrl C y to exit
#* * * * * /home/bnmr/vmic_online/perl/monitor.pl 0 1 1 1 1
#
  #
# run like this normally (change "10" to required proton current threshold) :
# run like this normally (change "10" to required proton current threshold) :
# Uncomment to turn on monitor
# Uncomment to turn on logging
# typical BL2A current is about 40 \mu A; set a threshold somewhat less.
# this editor is emacs: use C-x C-c y to save changes and exit
#
# typical BL2A current is about 40 \mu A; set a threshold somewhat less.
* * * * * /home/bnmr/online/perl/monitor.pl 35 1
#
</pre>
* * * * * /home/bnmr/vmic_online/perl/monitor.pl 35 1
 
 
The example above shows
The example above shows
<ul>
<ul>
<li>the script {{File|name=cycle_monitor_logfiles.pl}} run twice per day (cron parameters are minute=30 hour=8,20 i.e. 8.30am and pm)
<li>the shift program monitor.pl being run as a cron job every minute (cron parameters all =  *)
<li>the shift program monitor.pl being run as a cron job every minute (cron parameters all =  *)
with parameters
with parameters
<ul>
<ul>
<li>        minimum proton current  10
<li>        minimum proton current  35
<li>        suppress printing  TRUE
<li>        suppress printing  TRUE
</ul>
</ul>
Line 172: Line 181:
  $ crontab -e
  $ crontab -e
which will display the crontab file to be edited (see above).
which will display the crontab file to be edited (see above).
Comment out the two lines that starts the perlscripts, i.e.
Comment out the last line that starts the perlscript, i.e.
    # ...........
    #30 20 * * * /home/bnmr/vmic_online/perl/cycle_monitor_logfiles.pl 1
    ## * * * * * /home/bnmr/vmic_online/perl/monitor.pl 35 1
    # ...........
    # * * * * * /home/bnmr/vmic_online/perl/monitor.pl 10 1
and exit from the editor.
and exit from the editor.
== Rotate log files ==
The log files are rotated daily by logrotate. This is controlled by the file /etc/logrotate.d/musrmidas with the command
<pre>
/data1/bnmr/log/*.log {
        rotate 4
        monthly
        missingok
        nocreate
        notifempty
</pre>
See also [[BNMR: Perlscripts#Logrotate perlscript log files|logrotate perlscript logfiles]].


[[Category:BNMR]]
[[Category:BNMR]]

Revision as of 16:19, 31 August 2016

Links


Introduction

The BNMR shift monitor system consists of the perlscript monitor.pl in directory /home/bnmr/online/perl, which is run as a cron job during the beam period. It run as user bnmr, and accesses the required information from both the bnmr and bnqr ODBs.

The Shift Monitor program monitor.pl keeps a running total of how much usable beam time (in minutes) each TRIUMF experimental group has had access to, while running as experiment bnmr or bnqr (or both). It reads the proton current from EPICS once per minute. The information is stored in shift files in the directory /isdaq/data1/bnmr/mon/<year>/.

Output from the perlscript is sent to a logfile.

How it works

The perlscript /home/bnmr/online/perl/monitor.pl is run on isdaq01 (i.e. the midas host for bnmr and bnqr experiments) as a cron job (except for testing). Its output is written to a log file. It is run only as user bnmr, which has permission to access the EPICS variables needed.

The script determines which TRIUMF experimental group is running by reading the experiment number from the ODB parameter /Experiment/Edit on start/experiment number on both experiments. Access to the bnqr ODB is done using odbedit, i.e.

for bnmr odbedit -c 'ls "/Experiment/Edit on start/experiment number" '
for bnqr odbedit -h isdaq01:1177 -e bnqr -c 'ls "/Experiment/Edit on start/experiment number" '


The perlscript monitor.pl reads the proton current from EPICS (via caget "CCS2ISAC:BL2ACURRENT").

  • If it is below the threshold value (supplied as the first input parameter to monitor.pl), it exits.
  • Otherwise it reads from the EPICS switches which experiment(s) is/are enabled:
  • It reads the experiment number from the enabled experiment(s). The experiment number must be valid (0 < expt_num < 9999), otherwise no beam time will be allocated and an error will be reported
  • The program determines the name of the shift file by date and by 12-hour shift (AM/PM).
  • If no file of that filename exists, it is created and a line written for each different experiment number, and each running experiment assigned 1 minute of running time.
  • If bnmr and bnqr are both running with the same experiment number, only one line will be created with bnmr and bnqr experiments each given 1 minute accumulated time.
  • If there is an existing file, the program looks for a line with the experiment number. If it finds one, the accumulated time is incremented for the appropriate experiment. All other lines are copied and an updated file is written. If no line exists for this experiment number, a new line will be written.
  • If test mode is enabled (see #Input Parameters) an entry to the shift file written in test mode will be followed by an asterisk (see #Shift files) -- unless parameter skip file is true



Log files

The current log file is /isdaq/data1/bnmr/mon/monitor.log

When monitor.pl is running as a cron job, the output of the program can only be found in the log file, since printing informational messages to the screen must be suppressed (any messages printed are sent as email (see MAILTO in crontab)).

Logrotate (see #Rotate log files) is used to rotate the log files on a daily basis. 7 days worth of logfiles are retained.

Note : the shift files are never rotated or deleted unless by the user.

Shift files

Shift files are located in the directory /isdaq/data1/bnmr/mon/<year>/ and filenames are of the form

24Jul2014_AM.txt, 24Jul2014_PM.txt. The AM shift starts at 8.30am, the PM shift at 8:30pm.

The format of the shift files is shown in the following examples:

Example 1

This example shows one experimenter group (expt number 1604) has run on bnmr and two (1539,1419) on bnqr during an 8-hour shift.

# Shift Monitor File first opened at Sun Jun 12 20:30:01 2016
# Threshold(uA)   ExptNum        BNMR(min)    BNQR(min)  Time line written        Testing
   35.000         00001604       0643         0000       Mon Jun 13 08:28:01 2016    
   35.000         00001539       0000         0536       Mon Jun 13 06:32:01 2016    
   35.000         00001419       0000         0117       Mon Jun 13 08:29:01 2016    

Example 2 Test Mode

In this example monitor.pl has been run in test mode (the * under "Testing" indicates test mode - see #How it works). In this case, the same experiment numbers have been used for testing monitor.pl on both bnmr and bnqr.

 
 # Shift Monitor File first opened at Thu Jul 24 13:52:21 2014
 # Threshold(uA)   ExptNum        BNMR(min)       BNQR(min)   Time line written         Testing
   10.000         00001508        0080            0036        Thu Jul 24 15:50:01 2014    *
   10.000         00001412        0035            0021        Thu Jul 24 15:49:02 2014    *


Input Parameters

Table 1 : Input Parameters to monitor.pl
Input Parameter Explanation
1 Proton Current Threshold (uA) User selected value. If proton current is less than this value, program will exit without updating shift file.
In Test Mode, set value to 0 to skip reading proton current from EPICS.
2 Suppress printing Must be TRUE if running as a cron job
3 Test Mode Set Test Mode TRUE for testing if EPICS switches are set to "off"
4 BNMR enabled In Test Mode, set TRUE to enable BNMR regardless of EPICS switch
5 BNQR enabled In Test Mode, set TRUE to enable BNQR regardless of EPICS switch
6 skip file In Test Mode, set TRUE to prevent any update of shift files


NOTE
If not running in Test Mode, only the first two parameters are required.

Running the program by hand

The perlscript monitor.pl can be run by hand to check its operation. However, each time it is run, a minute will be added to the current experiment(s) in the shift files. Therefore, it should not be run by hand during the beam period (i.e. when the cron job is running) unless in test mode with the input parameter) skip file set true, to prevent any update of shift files.

To run the program, as user bnmr, in an xterm type e.g.

/home/bnmr/vmic_online/perl/monitor.pl  15  

Printing to the screen ( input parameter suppress printing) need not be set true when running by hand.

The program can also be run in test mode, e.g

/home/bnmr/vmic_online/perl/monitor.pl  5 0 1 0 1  

This would enable the bnqr experiment regardless of the EPICS switches (see #Input Parameters). If the proton current is less than the threshold, in test mode the script continues after writing a message to the log file. The shift file would be updated.

or it can be run in test mode with input parameter) skip file set true, to prevent any update of shift files:

/home/bnmr/vmic_online/perl/monitor.pl  0 0 1 1 1 1  

This would enable both experiments regardless of the EPICS switches. The proton current would not be read (see #Input Parameters), the shift files remain unchanged.


Running the cron job

As user bnmr, in an xterm type

$ crontab -l 

to list the crontab file, or to edit it

$ crontab -e

The crontab file will be displayed as follows:

# MAILTO=Gerald Morris <gmorris@triumf.ca>,suz@triumf.ca
SHELL=/bin/csh
# cron parameters :
# minute hour dayofmonth month dayofweek command
#
# Aug 31 2016  SD use logrotate
# logrotate now cycles the logfiles monitor.log. 
# See /etc/logrotate.d/musrmidas
#
# monitor.pl parameters: proton_current(uA) noprint testing bnmr_running bnqr_running skip_file
#
# run like this for TESTING cron job when epics switches are not set correctly and proton I is zero:
#
#use the odb to change the experiment numbers under "/experiment/edit on start/"
#* * * * * /home/bnmr/online/perl/monitor.pl 0 1 1 1 1 0
#
# Note:  emacs Cntrl X Cntrl C  y to exit
#
# run like this normally (change "10" to required proton current threshold) :
# Uncomment to turn on monitor
# typical BL2A current is about 40 \mu A; set a threshold somewhat less.
#
* * * * * /home/bnmr/online/perl/monitor.pl 35 1

The example above shows

  • the shift program monitor.pl being run as a cron job every minute (cron parameters all = *) with parameters
    • minimum proton current 35
    • suppress printing TRUE

Printing must be suppressed when running as a cron job, otherwise the MAILTO recipient will get long email messages every minute. If the last line is commented out, the cron job will not be running the monitor program (see stopping the cron job).


Stopping the cron job

As user bnmr, in an xterm type

$ crontab -e

which will display the crontab file to be edited (see above). Comment out the last line that starts the perlscript, i.e.

   ## * * * * * /home/bnmr/vmic_online/perl/monitor.pl 35 1

and exit from the editor.

Rotate log files

The log files are rotated daily by logrotate. This is controlled by the file /etc/logrotate.d/musrmidas with the command

/data1/bnmr/log/*.log {
        rotate 4
        monthly
        missingok
        nocreate
        notifempty

See also logrotate perlscript logfiles.