Forum: Cfengine Help
Subject: Re: How to extract values (not keys) from an array to slist?
Author: msvob...@linkedin.com
Link to topic: https://cfengine.com/forum/read.php?3,21174,21199#msg-21199

Hey Diego

Here's what I'm working on.  Hopefully, this either helps someone else trying 
to solve the same problem, or you could point me in a better direction.


    5   bundle agent rhel6_services
     6  {
     7  vars:
     8          redhat_6::
     9                  # running_service is what the cmd output is in the 
output of ps -ef in the process table.
    10                  # services we want running.
    11                  "running_service"                       slist   =>      
{ "ntpd", "mdadm", };
    12                                                                          
        
    13  
    14                  # services we dont want running.  this is what we would 
find in the output of ps -ef in the process table.
    15                  "stop_service"                          slist   =>      
{ "/usr/sbin/atd", "/usr/sbin/abrtd",};
    16  
    17                  # running_service_name is what the service is refered 
to via the chkconfig or service command.
    18                  # every entry from running_service needs to have an 
entry here.  Its what gets referenced in chkconfig and service cmds.
    19                  "running_service_name"                  string  =>      
"ntpd";
    20                  "running_service_name"                  string  =>      
"mdmonitor";
    21  
    22  
    23                  # stopped_service_name is what the service is refered 
to via the chkconfig or service command.
    24                  # every entry from stop_service needs to have an entry 
here.  Its what gets referenced in chkconfig and service cmnds.
    25                  "stopped_service_name"          string  =>      "atd";
    26                  "stopped_service_name"          string  =>      "abrtd";
    27  
    28  
    29  
    30  processes:
    31          # Only raise these classes if we are on a RHEL6 host.  Once the 
class is defined, RHEL6 is an 
    32          # assumed prerequesite.  the running_service variable is 
expanded from an slist to an individual entry, and then
    33          # that individual entry is actually checked in the process 
table.  If its not found,
    34          # we raise the "restart" class.  This class gets executed upon 
in the commands section.  
    35          redhat_6::
    36                  "$(running_service)"
    37                          restart_class   =>      
canonify("$(running_service)_start");
    38  
    39          # If we found a service in the process table that we dont want 
running, then execute its stop function using service..
    40          redhat_6::
    41                  "$(stop_service)"
    42                          process_stop    =>      "/sbin/service 
$(stopped_service_name[$(stop_service)]) stop";
    43  
    44  commands:
    45          ################################# START SERVICES 
###############################################
    46          redhat_6::
    47                  "/sbin/service 
$(running_service_name[$(running_service)]) start"
    48                          ifvarclass      =>      
canonify("$(running_service)_start");
    49  
    50          # Since we had to start this process, make sure the symlinks 
are in place via chkconfig.
    51          #redhat_6::
    52                  #"/sbin/chkconfig 
$(running_service_name[$(running_service)]) on"
    53                          #ifvarclass     =>      
canonify("$(running_service)_start");
    54          ################################# START SERVICES 
###############################################
    55  
    56  methods:
    57          redhat_6::
    58                  "any"   usebundle       =>      
rh_chkconfig_on($(rhel6_services.running_service_name));
    59  
    60  reports:
    61          redhat_6::
    62                  "cf3: RHEL6 service 
$(running_service_name[$(running_service)]) was restarted on $(sys.host)"
    63                          ifvarclass      =>      
canonify("$(running_service)_start");
    64  
    65          redhat_6::
    66                  "cf3: RHEL6 service 
$(stopped_service_name[$(stop_service)]) was disabled on $(sys.host)"
    67                          ifvarclass      =>      
canonify("$(stop_service)_stop");
    68  }
    69  ###################################################
    70  bundle agent rh_chkconfig_on(service)
    71  {
    72  vars:
    73          "index"         slist   =>      getindices("service");
    74  
    75  reports:
    76          redhat_6::
    77                  "Service is ${index}";
    78                          
    79  }




So the problem I'm running into here, is creating the relationship between the 
process name in the process table from the output of ps -ef, and its service 
name...

The bulk of code above just handles starting / stopping the processes.  Going 
from an example that Neil provided:



       vars:
            unxxhd01::
            "services" slist => {
                "xfs",
                "smb",
                "autofs",
                "xinetd"


        methods:

            redhat|SuSE::

            "any" usebundle => rh_chkconfig_status(${services});
}

bundle agent rh_chkconfig_status(s){
# Report whether service is enabled using chkconfig

    classes:
    
        "${s}_enabled" expression =>
             returnszero("/sbin/chkconfig ${s}","noshell");

    reports:

        all::
            "${g.pf} Service ${s} is enabled",
            ifvarclass => "${s}_enabled";

            "${g.pk} Service ${s} is not enabled",
            ifvarclass => "!${s}_enabled";
}




So, this works for chkconfig, but there isn't a relationship between the 
process name, and what its service / chkconfig name is called.  By creating the 
array, I lock those two values together. 

Unfortunately, to extract the values of the things I need to chkconfig on / 
off, I need the getindicies() equiv that Mark has just submitted to the SVN 
trunk.

The methods: part, and rh_chkconfig_on promise have been modified, as I started 
trying other solutions.  I tried to pass the array in the method call, but I 
think i'm going to give up on that approach.

I think as a work around, I'll have the service name act as the key, and the 
process table name be the value.  That will allow me to work with the process: 
part of the policy, and then be able to feed an slist to the method using 
getindicies()

And thanks again Mark for the new function.  =)

Mike

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to