5 Replies Latest reply on Jul 7, 2015 12:52 PM by Dale Ferrier

    Discovery.getfile from windows hosts

    Dale Ferrier
      Share This:

      I am examining our discovered hosts for a file that will set the Organizational Unit for the host if the file exists and if it does not exist we have defined a lookup table to set the OU for a particular host.

       

      This file lookup works fine for unix based hosts but I can't get it to work for our windows hosts. Please see the tpl code below.

       

      Is there something different that we must do in order to read a simple text file from a windows server? I have checked the permissions and any authenticated user has read access to the file.

      -------------------

      // Pattern to set Organisational Units based on a key file

      // stored here:  UnixcfgfilePath on Unix hosts or WincfgfilePath on Windows hosts

      //

      // OR

      //

      // If the key file is not found then a lookup table is

      // consulted

      //

      tpl 1.10 module OrgUnit_Table_and_File;

       

      // This table is used to find organisationalunit names (i.e. the "name" attribute on

      // OrganisationalUnit nodes) by looking up a organisationalunit id extracted from the

      // scanner name.

       

      // Format the table as follows:

      // Add entries for scannername as needed

      // Note that this is only needed on the consolidator

      // table ScannerToOrgUnit 1.0

      //   'Scannername1' -> 'OrganizationalUnit';

      //   'Scannername2' -> 'OrganizationalUnit';

      //   default -> ''; 

      // end table;

       

      // Servers we are consolidating

       

      table ScannerToOrgUnit 2.1

          'p3qaddm1' -> 'FRIT';

          'p3paddm1' -> 'FRIT';

          'p1paddm1' -> 'FRIT';

          'p1paddm4' -> 'FRIT';

          'p2paddm1' -> 'FRIT';

          'GCS Dallas ADDM Scanner'   -> 'GCS';

          'Dallas DMZ ADDM Scanner'   -> 'GCS';

          'GCS EROC ADDM Scanner'     -> 'GCS';

          'GCS EROC DMZ ADDM Scanner' -> 'GCS';

          default -> 'OU not determined';

      end table;

       

      pattern OrgUnit_Table_and_File 2.1

        '''

          Pattern to set the Organisational Unit found in a text file on a given host, stored here: UnixcfgfilePath

         

          If the text file is not found then use the ScannerToOrgUnit lookup table.

        '''

       

        overview

          tags OrganisationalUnit;

        end overview;

       

        constants

       

          UnixcfgfilePath := "/tmp/OUFile.txt";

          WincfgfilePath := "c:\temp\OUFile.txt";

         

        end constants;

         

        triggers

          on host := Host created, confirmed;

        end triggers;

       

        body

            // Check for OU key file, Unix

           

            Unixcfgfile := discovery.fileGet(host, UnixcfgfilePath);

           

            if Unixcfgfile

            then

              log.debug('OrgUnit_Table_and_File: "%Unixcfgfile%" found');

              // Look up the organisationalunit name from the file Unixcfgfile

              OU_name := Unixcfgfile.content;

       

              // Find the OrganisationalUnit node(s) for the organisationalunit

              ou := search(OrganisationalUnit where name = '%OU_name%');

                 

              // the OU does not currently exist in ADDM so create it

              if not ou

              then

                  ou := model.OrganisationalUnit(name := OU_name, key := OU_name);

                  log.info('OrganisationalUnit node created for "%OU_name%"');

              else

                  log.debug('OrganisationalUnit "%OU_name%" was found');

              end if;

         

              // Relate host to OU

              model.uniquerel.Ownership(OwnedItem := host, Owner := ou);

              stop;

            end if;

           

            // Check for OU key file, Windows

           

            Wincfgfile := discovery.fileGet(host, WincfgfilePath);

           

            if Wincfgfile

            then

              log.debug('OrgUnit_Table_and_File: "%Wincfgfile%" found');

              // Look up the organisationalunit name from the file Wincfgfile

              OU_name := Wincfgfile.content;

             

              // Find the OrganisationalUnit node(s) for the organisationalunit

              ou := search(OrganisationalUnit where name = '%OU_name%');

                 

              // the OU does not currently exist in ADDM so create it

              if not ou

              then

                  ou := model.OrganisationalUnit(name := OU_name, key := OU_name);

                  log.info('OrganisationalUnit node created for "%OU_name%"');

              else

                  log.debug('OrganisationalUnit "%OU_name%" was found');

              end if;

             

              // Relate host to OU

              model.uniquerel.Ownership(OwnedItem := host, Owner := ou);

              stop;

            end if;

           

            // use the lookup table if no key file

       

            scanners := search(in host traverse InferredElement:Inference:Associate:DiscoveryAccess traverse Member:List:List:DiscoveryRun);

            scanner := '';

            if scanners

            then

              scanner := scanners[0]._consolidation_source_name;

              log.debug('OrgUnit_Table_and_File: "%scanner%" found');

            else

              log.info('OrgUnit_Table_and_File: No consolidation source found');

            end if;

           

            // Look up the organisationalunit name from the table

            OU_name := ScannerToOrgUnit[scanner];

            if not OU_name

            then

              log.info('OrgUnit_Table_and_File: Unknown organisationalunit, based on scanner name "%scanner%"');

              stop;

            end if;

           

            // Find the OrganisationalUnit node(s) for the organisationalunit

            ou := search(OrganisationalUnit where name = '%OU_name%');

            if not ou

            then

              ou := model.OrganisationalUnit(name := OU_name, key := OU_name);

              log.info('OrgUnit_Table_and_File: OrganisationalUnit node created for %OU_name%');

            else

              log.debug('OrgUnit_Table_and_File: OrganisationalUnit %OU_name% was found');

            end if;

           

            // Relate host to OU

            model.uniquerel.Ownership(OwnedItem := host, Owner := ou);

            

        end body;

      end pattern; 

        • 1. Re: Discovery.getfile from windows hosts
          Andrew Waters

          No - there is no fundamental difference between discovery of Windows or Unix. What is being reported on the DiscoveryAccess page when trying to access the file on Windows? If it is getting the content perhaps you need to use text.strip() it to remove any extraneous white space.

           

          I would suggest that you check os_class attribute on Host as this allows you to easily distinguish Windows and Unix machines without trying to get the Unix file from a Windows machine.

          if host.os_class = "Windows" then

            // Windows

          else

            ...

          • 2. Re: Discovery.getfile from windows hosts
            Dale Ferrier

            Thanks Andrew, yes that is what my assumption was that the function should work the same whether it is a windows host or unix host.

             

            The file is not being found at all. No information is being reported and the table lookup is being used. As you can see in the code if one or the other files are found on a host I have a stop; command to exit the tpl at that point.

             

            I will consider the host.os_class enhancement you suggest above. It should save one call to discovery.getfile at least.

            • 3. Re: Discovery.getfile from windows hosts
              Dale Ferrier

              Oh man I am almost embarrassed to post this. I found the problem...

               

              Here is what I was looking for:

               

              WincfgfilePath := "c:\temp\OUFile.txt";

               

              Here is the actual path:

               

              c:\Temp\OUFile.txt

               

              Notice that Temp is capitalized... man what a noob mistake... I changed the variable to:

               

              WincfgfilePath := "c:\Temp\OUFile.txt";

               

              And all was well.

               

              I was assuming that case did not matter... You know what they say about assuming anything?

               

              assume: makes an "ass" out of "u" and "me".

               

              So anyway, hopefully this helps someone...

              1 of 1 people found this helpful
              • 4. Re: Discovery.getfile from windows hosts
                Andrew Waters

                It is not the case, it is that \ marks escape characters so you need to either use

                WincfgfilePath := raw "c:\temp\OUFile.txt";

                or escape the backslashes

                WincfgfilePath := "c:\\temp\\OUFile.txt";

                • 5. Re: Discovery.getfile from windows hosts
                  Dale Ferrier

                  Ok thanks Andrew.