pro disk_monitor, disk, limit, infile=infile, nomail=nomail, $
	nowarn=nowarn, loud=loud, hc=hc, mail=mail, expand=expand, noexpand=noexpand
;
;+
;   Name: disk_monitor
;
;   Purpose: monitor disk space on critical disks (run via cron job)
;
;   Input Parameters:
;
;   Keyword Parameters:
;      infile - if set, use input file (def=$DIR_SITE_SETUPD/disk_monitor.dat)
;      loud   - if set, report nominal disks (def=warning and critical)
;      hc     - if set, produce hardcopy
;      nomail - if set, dont mail message	(default for interactive)
;      mail   - if set, mail message 		(default for batch)
;
;   Calling Sequence:
;      disk_monitor [infile=infile, /nomail , /loud , /hc]
;
;   History:
;      22-Feb-1993 (SLF) 
;       8-Jun-1994 (SLF) - look for bad status (-1) from diskfree.pro
;			   add EXPAND & NOEXPAND keyword and reformat
;-

expand=1-keyword_set(noexpand) 		; SLF, I made expand the default...

; set mail status (keyword or mode)
mailit = (keyword_set(mail)) or $
	(keyword_set(get_logenv('ys_batch')) and 1-keyword_set(nomail))

loud=keyword_set(loud)

if n_elements(infile) eq 0 then infile= $
	concat_dir('$DIR_SITE_SETUPD','disk_monitor.dat')

if n_elements(disk) eq 0 then begin
   inp=rd_tfile(infile,/nocomment,6)
   disk=reform(inp(0,*))
   warn=reform(inp(1,*))
   red_alert=reform(inp(2,*))
   users=reform(inp(3,*))
   comments=reform(inp(4,*))
   comment=reform(inp(5,*))
endif else begin
   message,/info,'Input not yet supported, using file:'
   message,/info,concat_dir('$DIR_SITE_SETUPD','disk_monitor.dat')
   disk_monitor,nomail=nomail,  loud=loud
endelse

head=['  STATUS    Available   Limit          Disk     Mount Name  '] ; [description]']
head=['','------ disk_monitor run at: ' + systime(),'',head,'']
summfree=''
nohead=1
for i=0,n_elements(disk)-1 do begin
   odisk=str2arr(disk(i))
   for ii=0,n_elements(odisk)-1 do begin
      if ii eq 0 then odisks=''
      if strpos(odisk,'$') eq 0  then begin
         odisks=[odisks,get_logenv(strmid(odisk(ii),1,100)) ]
      endif else begin
         odisks=[odisks,odisk(ii)]
      endelse
   endfor
   odisks=odisks(1:*)

;  should have done the following in the first place to simplify this routine!!
   if keyword_set(expand) then begin		; expand shell prior to diskfree
      otemp=''
      for od=0,n_elements(odisk)-1 do begin	; use shell to expand 
         spawn,'/bin/ls -d ' + odisk(od),out	; *** only verified on ultrix...
         otemp=[otemp,out]
      endfor
      odisks=otemp(1:*)
   endif
   
   odisks=strtrim(odisks,2)
   for jj=0,n_elements(odisks)-1 do begin
      if odisks(jj) ne '' then begin
      free=diskfree(odisks(jj),use=use,mount=mount,filesys=filesys)
      for j=0,n_elements(free)-1 do begin
      if mount(j) ne '/' then begin
      sayit= keyword_set(loud)
      critical=0
      if nohead then begin
         prstr,head
         nohead=0
      endif
      case 1 of
	 free(j) eq -1: begin
	    mess='BAD STAT: ' $
		+ string(free(j),format='(f6.1," Mb")') + $
		 string(warn(i),format='(" <",f5.1," Mb>")') + ' >>> ' + $
		 string(odisks(jj),mount(j), format='(a6,"...",2x,a)')
	    summfree=[summfree,mess]	    
	    message,/info,mess,/noname
         endcase
;        should consolodate these prints/formats if you have time...
         free(j) lt red_alert(i): begin
            mess='CRITICAL: ' $
		+ string(free(j),format='(f6.1," Mb")') + $
		 string(red_alert(i),format='(" <",f5.1," Mb>")') + ' >>> ' + $
		 string(odisks(jj),mount(j), format='(a6,"...",2x,a)')

	    message,/info,mess,/noname
	    if comment(i) ne '' then mess=[mess,comment(i)]
	    if mailit then $
	       mail,[head,mess],subj='URGENT: Critical Disk Space Situation',/no_def
	    summfree=[summfree,mess]	    
	    critical=critical+1
            endcase
         free(j) lt warn(i): begin
            mess='WARNING:  ' $
		+ string(free(j),format='(f6.1," Mb")') + $
		 string(warn(i),format='(" <",f5.1," Mb>")') + ' >>> ' + $ 
		 string(odisks(jj),mount(j), format='(a6,"...",2x,a)')
	    message,/info,mess,/noname
	    summfree=[summfree,mess]	    
	    endcase
          else: begin
             mess='NOMINAL:  ' $
		+ string(free(j),format='(f6.1," Mb")') + $
		 string(warn(i),format='(" <",f5.1," Mb>")') + ' >>> ' + $ 
		 string(odisks(jj),mount(j), format='(a6,"...",2x,a)')
	     if loud then begin
                 message,/info,mess,/noname
                 summfree=[summfree,mess]
             endif
          endcase
       endcase
      endif
     endfor			; spagetti (environmentals, csh expansions, etc)
    endif
   endfor
endfor

if n_elements(summfree) gt (1 + critical) and mailit then begin
   musers=arr2str(users(uniqo(users)),', ')
   summfree=summfree(1:*)
   subj='Warning: Low Disk Space '
   bstat=where(strpos(summfree,'BAD STAT') ne -1,bscnt)
   case 1 of 
      bscnt eq n_elements(summfree): subj='BAD MOUNT STATUS'	; only mount
      bscnt gt 1: subj=subj + '& BAD MOUNT STATUS'		; low & mount
      else:							; only low
   endcase
   mail,[head,summfree],user=musers, subj=subj ,/no_defsub
endif
   
if keyword_set(hc) then prstr,[head,summfree],/hc

return
end
