Introduction
Custom Inventory in Notification Server 8.x and later is cross-platform and script-based. The same console screens are used to create and configure custom inventory across all platforms - Windows, Unix, Linux and Macintosh. However, the code is not cross-platform. Custom inventory scripts must be written in a language that can be executed on each OS. The following scripting languages are supported:
- JavaScript
- Perl
- PowerShell
- Python
- Unix Shell script
- VBScript
Note that while 'Run Script' tasks can be run in additional languages, only the languages listed here are capable of processing custom inventory logic and objects. Several sample custom inventory scripts are attached to this article.
Custom Inventory requires that the data class that will hold the custom data be created on the Notification Server prior to deploying the custom inventory task to clients. These data classes are referred to as 'custom data classes'.
The basic process of creating a custom inventory task is:
- Create the data class in the NS console
- Create the task with scripting logic in the Notification Server console
- Deploy the custom inventory task to clients, which send results back to the NS
- Create a report to view the results of the custom inventory. (This document will not discuss report creation.)
Create a Custom Data Class
- In the NS Console, click Settings, All Settings, . Then, expand Discovery and Inventory, Inventory Solution, Manage Custom Dataclasses.
- Click on "New Dataclass" and enter a Name and Description. In this example, I will be selecting data from the Win32_UserAccount class and creating attributes for the following three fields: Name, Disabled, and SID. Note: After saving changes in step 7 below, the system will create a table in the CMDB with the data class name you entered prefixed by Inv_ (Inv_Win32_UserAccount for example).
- Highlight the new dataclass and click "Add attribute". Enter a name and select or enter the appropriate Data Type, Maximum size, Key option and Data required option.
- Repeat this for each attribute/column you wish to collect data for.
- Note the option to 'Allow multiple rows from a single computer resource'. This is helpful, and necessary, as is the case with a User report, for example, where multiple users can be reported on each computer.
- When you are done adding attributes, click "Save changes" at the bottom of the screen.
- The GUID for this dataclass will be required to be entered in the script. To find the guid, click on a dataclass name, then click the "Details" button, which is the little hand between the "New dataclass" button and the red "X" button. Make a note of the guid value and save it for later use. (Note: this is a Windows-only requirement. Unix, Linux and Macintosh scripting do not have this requirement.)
Create a Custom Task and Script for Windows
The easiest way to create a new custom inventory task is to clone a sample. The sample already contains the required code for a WMI query. You will only need to add your own logic to gather the data you require and to populate the attribute variables in the script.
For this example, we will walk through the steps required to create a new task and manually enter the custom inventory code. This code can be copied from a sample, or previously existing, custom inventory task prior to creating this new task.
- In the NS Console, click on 'Manage' > 'Jobs and Tasks' and click on the 'Jobs and Tasks' menu entry in the left-hand pane.
- Click on 'Create a new job or task' in the right-hand pane. A new window will open.
- Scroll to the bottom of the left-hand pane in the new window and click on 'Run Script' to select the appropriate type of task.
- Enter an appropriate name for this task in top text field.
- Select the desired script type. Again, Custom Inventory in NS can run AppleScript, Command Script, JavaScript, Perl, Powershell, Python, Unix shell scripts or VBScript.
- Enter the following code. A similar code will already exist if you cloned the task. This task performs the following:
- Creates a wmi object, executes a wmi query and stores the result set.
- Creates an Altiris NSE object.
- Creates an Altiris 'Inventory data block' and associates it with a specific custom data class.
- Loops through each row in the result set and populates each row of the result set into a row in the datablock.
- Processes and sends the NSE to the Notification Server.
On Error Resume Next
'Create instance of Wbem service object, connect to namespace and run wmi query
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objCIMObj = objWMIService.ExecQuery("select * from win32_useraccount")
'Create instance of Altiris NSE component and set the header data of the NSE
dim nse
' The Altiris.AeXNSEvent class is installed with the inventory plugin and can be referenced externally.
set nse = WScript.CreateObject ("Altiris.AeXNSEvent")
' Please don't modify this GUID. This is the 'Inventory Capture Item'.
nse.To = "{1592B913-72F3-4C36-91D2-D4EDA21D2F96}"
nse.Priority = 1
'Create Inventory data block for this dataclass.
'Specify either the name or the guid of the associated dataclass. The name is recommended.
dim objDCInstance
'set objDCInstance = nse.AddDataClass ("{3ce8a021-ffaa-4467-a01a-872e0fcabbdf}")
set objDCInstance = nse.AddDataClass ("WinUsers")
dim objDataClass
set objDataClass = nse.AddDataBlock (objDCInstance)
For each objInfo in objCIMObj
'Add a new row and set column values
dim objDataRow
set objDataRow = objDataClass.AddRow
objDataRow.SetField 0, objInfo.Name
objDataRow.SetField 1, objInfo.Disabled
objDataRow.SetField 2, objInfo.SID
Next
nse.Send
- Note that custom inventory includes the ability to add 'tokens', or pre-defined variables, to scripts.
- It is also possible to create new tokens. The values can be selected from the Symantec_cmdb database. '%COMPUTERID% is a predefined token that contains the client's guid value.
- The advanced tab contains settings for authentication, windowing of the client task, and task control.
- Click 'Save Changes' to save the script and associated settings. At this point, the task is ready to run.
- The 'Task Status' section of this screen contains scheduling and other options. Scheduling is partly controlled by core NS functionality and partly by Inventory Solution functionality.
- Quick Run - When this option is selected, a combo list box is presented. The desired client computer name can be entered. Also, a drop-down list of available resources will be populated and the desired resource can be chosen from the list. Selecting a resource and clicking 'run' will initiate the task on the selected client immediately, depending on maintenance windows, etc.
- New Schedule - This is a standard scheduling window. A new schedule will be associated with the current task, allow for selection of specific resources, and can be ran 'Now' or on a specified schedule that has the option to repeat at a specified frequency. There is also an option to override maintenance windows.
- Other features in the 'Task Status' section are the task details screen, delete task entries, task history view, grouping and search options and the refresh button.
- Details - This is the little hand icon. It shows details of when the highlighted task ran, if it succeeded, etc.
- Delete - The red 'X' allows you to delete highlighted entries in the task status section.
- View - Allows for seeing current or archived entries.
- Group By - Provides a drop-down list for grouping entries to see a higher-level view.
- Search - Allows for searching for specific entries.
- Refresh - Reloads entries and status of entries. The down-arrow allows for setting an automatic refresh interval.
Create a Custom Task and Script for Unix, Linux or Macintosh
Creating a Unix or Linux custom inventory task is exactly the same as creating a custom inventory task for Windows except for the scripting language and the actual code, or logic, used in the script. The Unix and Linux custom inventory in NS7 was built to facilitate the re-use of custom inventory logic from an NS6 environment.
- Create a custom data class, as explained above.
- Create a new task, as explained above. Or, simply browse to "Jobs and Tasks -> Samples -> Custom ->Custom Inventory - PS List", right-click on this task and select clone.
- Add and modify the code of your new task as required for your specific requirements. See the sample code and explanation, below.
- After saving the task, it may be helpful to move it to a more appropriate folder in the menu tree. Simply open the desired location in the menu tree and drag the task to that location.
- The task can be run and scheduled the same as any other task in the NS console.
Following is a sample shell script for Unix, Linux and Macintosh custom inventory.
## Sample script for Unix, Linux and Macintosh custom inventory
. `aex-helper info path -s INVENTORY`/lib/helpers/custominv_inc.sh
# The custominv_inc.sh inclusion is required at the begin of the script, prior to any other custom inventory logic.
# The following comment/label is also required to mark the beginning of the custom inventory logic.
# SCRIPT_BEGINS_HERE
#!/bin/sh
echo UNIX_PS_List
echo "Delimiters=\" \" "
echo string20 string20 string20 string256
echo PID Terminal Time Command
if [ "`uname -s`" = "Darwin" ] ; then
ps -ax | sed -e "1d" | awk '{print $1 " " $2 " " $4 " " $5 " " }'
else
ps -e | sed -e "1d" | awk '{print $1 " " $2 " " $3 " " $4 " " }'
fi
This code does the following:
- Include a file required to format the data.
- Specify the data class.
- Specify delimiters for use in parsing the data returned from the command to be executed.
- Specify the datatypes and lengths of each column.
- Specify the column names. Note: this is only required when the command to be executed does NOT already include column headings.
- Run the desired command. In this case, we check for and run appropriate platform-specific commands.