Saturday, August 19, 2017

puppet :: How to run bginfo via the scheduler

I won't go in to details about bginfo. There are plenty of articles on the internet about bginfo. I won't go in to details about the scheduler either for the same reason. In this blog post, I will explain how I use puppet to tell the scheduler to run bginfo.

The first thing you need to do is to install bginfo.exe somewhere and then configure bginfo to display the statistics you want. Then, create a scheduled task to run bginfo.exe at logon (for what user or maybe all users who log on). You can also tell the scheduler to execute every 5 minutes while the user is logged on. That way, while the user is logged on, the information stays fresh and if the user is not logged on, don't waste cpu cycles.

Now that you've got it running successfully on one of your servers, all you need to do is export your bginfo xml template file and your scheduled task. Place those two files on your puppet server.


In your puppet module, use this code to import the scheduled task.

 file {
    "c:\\sbin":
      ensure => directory,
  }

  file {
    "c:\\sbin\\Bginfo.exe":
      ensure  => present,
      require => File["c:\\sbin"],
      source  => "puppet:///modules/sysinternals/Bginfo.exe",
  }

  file {
    "c:\\sbin\\mycustomFile.bgi":
      ensure => present,
      require => File["c:\\sbin\\Bginfo.exe"],
      source  => "puppet:///modules/sysinternals/mycustomFile.bgi",
  }

  file {
    "c:\\sbin\\bginfo.xml":
      ensure  => present,
      require => File["c:\\sbin\\mycustomFile.bgi"],
      source  => "puppet:///modules/sysinternals/bginfo.xml",
  }

  exec {
    "schtasks_bginfo":
      command => "c:\\Windows\\System32\\schtasks.exe /Create /XML c:\\sbin\\bginfo.xml /TN bginfoFG2",
      unless  => "c:\\Windows\\System32\\schtasks.exe /Query /TN BginfoFG2",
      require => File["c:\\sbin\\bginfo.xml"],
  }



puppet :: How to copy a file only if an account exists

I scoured the internet to see if anyone had already done something like this. I couldn't believe that I found nothing. The only bits I found was for, if another file exists or if a directory exists.

Anyway, this is how I did it.

First step I did was to create a custom fact. I named the custom fact isoracle. Then, in my class, I checked to see if isoracle true or false. If isoracle true, I copied the file.

The custom fact goes in this file:
./modules/files4all/lib/facter/isoracle.rb

The contents of the file is:
Facter.add(:isoracle) do
  setcode do
    isoracle = Facter::Util::Resolution.exec('id oracle 2>/dev/null')
    if isoracle == ""
      isoracle = false
    else
      isoracle = true
    end
  end
end

The class file goes in this file:
./modules/files4all/manifests/oracle.pp

The contents of this file is:
class files4all::oracle {

#Check the custom fact to see if oracle account exists
#skip if oracle account does not exists
  if $isoracle == true {
#These are the defaults unless overridden
    File {
      ensure => "present",
      owner  => "oracle",
      group  => "oracle",
      mode   => "0644",
    }

    file {
      [ "/oracle", "/oracle/sbin" ]:
      mode   => "0755",
      ensure => "directory",
    }

    file { "/oracle/testfile":
      source => "puppet:///modules/files4all/oracle/testfile",
    }

  }

}

What will happen is, puppet will first copy the isoracle.rb file to the client and run it. From there, puppet will be able to determine if the oracle account exists or not. I suppose you could modify the code to check for anything you want.