#!/usr/bin/perl
# $Id: cfmh,v 1.12 2000/09/05 18:04:14 dmetz Exp $
# $crtd:  by  Derald Metzger  on  990625 $
# $cmnt:  cfm host reference full scan
#           Copyright (C) 1998 - 2000 Derald Metzger
#   This may be freely redistributed under the terms of the GNU GPL.
# $

#  Load perl modules
use Cfm;
use File::Basename;

# Id/set vars
$base = basename($0);
$mon_only = 0;     # Only update the monitor files
$dst_fil;          # Output destination file
($hnam) = (`hostname` =~ /(\w+).*/);
@opts = "-v1";     # Default cfm options
$sdate = `date +%y%m%d`; chop $sdate;  # short date yymmdd

$usage="
NAME
  cfmh  - cfm host reference full scan

SYNOPSIS
  cfmh -m .
  cfmh [-f dst_fil] { cfm_options | . }

DESCR
  The `cfm -m .' invocation simply updates the monitor files chk.cfm,
  fdisk-l, and rpm-qa.sort. These reflect the current boot time daemon
  config, the disk partition tables, and the rpm complement on the
  executing host respectively.

  The 2nd invocation is essentially an options wrapper for cfm.  It
  updates the monitor files.  It then tests config files that exist on
  the executing host against those in the cfm database.  The files
  list is built by `cfmhrlis` to include all files in the /etc dirtree
  plus the complete set of config files identified by rpm.  This list
  is filtered to remove volatile or otherwise inappropiate files.
  Note that this cmd will not tell you about cfm database files you
  are missing. Use the cfmd cmd for that.

OPTIONS
  -f dst_file  - destination file, eg: /cm/cfm/status/\`hostname`_h
  -m           - only update the chk.cfm, fdisk-l, and rpm-qa.sort files
  cfm_options  - see cfm, default options are -Rq
  .            - execute with no args vice display usage info

FILES
  /etc/rcd./init.d/chk.cfm  Output of `chkconfig --list' at execution time
  /etc/cfm/fdisk-l          Output of `fdisk -l' at execution time
  /etc/cfm/rpm-qa.sort      Output of `rpm -qa | sort' at execution time

ALSO
  See also cfm-cmd-list.

AUTHORS
           Copyright (c) 1998 - 2000  Derald Metzger 
  This file is part of the cfm pkg (GPL).
";

# Get the $dst_fil opt.
while( $ARGV[0] =~ /^(-f|-m)/ ) {
    $_ = shift;
	/-m/ && do {
		$mon_only = 1;
		next;
	};
    /-f(?:$|(\S+$))/ && do {
        ($1 && ($_ = $1)) || ($_ = shift);
        $dst_fil = $_;
		next;
    };
}

# Arg or output usage
@ARGV || die "\n### $0: insufficient args $usage";

# Get cfm opts
!($ARGV[0] eq "\.") && (@opts = @ARGV);

# Update the monitor files
`echo "# written/rewritten by cfmh" >/etc/rc.d/init.d/chk.cfm`;
`chkconfig --list | sort >>/etc/rc.d/init.d/chk.cfm`;
`echo "# written/rewritten by cfmh" >/etc/cfm/rpm-qa.sort`;
`rpm -qa | sort >>/etc/cfm/rpm-qa.sort`;
`echo "# written/rewritten by cfmh" >/etc/cfm/fdisk-l`;
`fdisk -l >>/etc/cfm/fdisk-l`;
$mon_only && exit;

# Open destination file if we need it
$dst_fil && (open(CFMHOUT, ">$dst_fil") || die "### Can't open $dst_fil");

# Feed the full,sorted,unique list of cfg files on this host to cfm
open(CFMH, "nice cfm @opts `cfmhrlis` 2>&1 |");
while(<CFMH>) {
    /^## $sdate $hnam/ && s/Summary Report/cfmh report/o;
    if(!$dst_fil) { print; }
    else { print CFMHOUT; }
}
close CFMH;
    
close CFMHOUT;
