Absent Member.. Timtator Absent Member..
Absent Member..
2417 views

UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi everyone,

 

New to the forum 🙂

 

I have recently created a custom date attribute on the Node CI Type called "last_update_from_source".

 

I am editing the WMI_Connection_Util.py script and attempting to get the current time on the node, format is to look like "Oct 9, 2013 12:00:00 PM" and then updating the custom attribute.

 

This is my first time playing with python and WMI. The discovery runs without issue, there is no errors. However, the field does not update. I have tried with a string attribute as well and it still won't work.

 

Question 1. Have any of you had to do anything similar?

Question 2. Is this really the most effective way to populate the field?

Question 3. Can you see why it isn't working?

 

 _wmiQuery5 = 'SELECT Day, Hour, Minute, Month, Year FROM Win32_LocalTime'
	    resultSet = client.executeQuery(_wmiQuery5)  # @@CMD_PERMISSION wmi protocol execution
	    if resultSet.next():
                Day = resultSet.getString(1)
                Hour = resultSet.getString(2)
                Minute = resultSet.getString(3)
                Month = resultSet.getString(4)
                Second = resultSet.getString(5)		    
                Year = resultSet.getString(6)	
                if Month == 1:
                    Month = 'Jan' 
                elif Month == 2:
                    Month = 'Feb'
                elif Month == 3:
                    Month = 'Mar'
                elif Month == 4:
                    Month = 'Apr'
                elif Month == 5:
                    Month = 'May'
                elif Month == 6:
                    Month = 'Jun'
                elif Month == 7:
                    Month = 'Jul'
                elif Month == 8:
                    Month = 'Aug'
                elif Month == 9:
                    Month = 'Sep'
                elif Month == 10:
                    Month = 'Oct'
                elif Month == 11:
                    Month = 'Nov'
                elif Month == 12:
                    Month = 'Dec'
			
            DateTime = Month + ' ' + Day + ', ' + Year + ' ' + Hour  + ':' + Minute + ':' + Second + ' AM'
            hostOSH.setAttribute('last_update_from_source', DateTime)
            
        vector.add(hostOSH)

 

Thanks in advance.

 

Tim

 

 

 

0 Likes
17 Replies
Acclaimed Contributor.. Dima Gomel Acclaimed Contributor..
Acclaimed Contributor..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi Tim,

I'd like to comment on the question.

1. Changing of OTTB scripts is not recommended. Every CP update will overwrite your changes. Please copy and rename scripts that you need. Than it will be safe.

2. Why do you need local time on the system be populated? This time won't show you the time of the change. It will show time of discovery in local timezone. In case you're trying to find the timezone on remote destination, in my opinion, it should be done in a different manner. In all other cases, I see no difference between your way and OTTB "Last ModifiedTime" or "Create Time" attributes. Please elaborate on your goals.

 

Regards
-Dmitry Gomel, PMP
Click the Like button at the bottom to say 'Thanks'.
0 Likes
Absent Member.. Timtator Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi sorry, for the massive delay. I have made quite a bit of progress since my original post.

 

I am aware that modifying the scripts is not advised, but we don't really have any other options.


I will probably be better off explaining the problem so that I can find a proper solution.

 

Aging in UCMDB uses the last_modified_time attribute to determine if a CI is a candidate for aging.

 

The problem we have is that our Windows servers are discovered using our Data Flow Probe.

 

In an ideal world, any servers not discovered would be marked for aging and then would be deleted at a later date.

 

Unfortunatly, we have a Connect IT scenario that updates the Windows CI with purchase information from our clients asset tracking system. This then changes the last_modified_date, even if the CI is no longer in use in the enviroment.

 

As the servers are sometime just moved, renamed and reutelised, there is no accurate way to determine whether the server is still in use.

 

This is why we determined we needed a new custom attribute, that could be used to determine a servers last discovery. This means we need to modify the NTCMD, WMI and UNIX discovery scripts to set the last_update_from_source to the current date and time. We can then use an enrichment rule to delete CI's after a set period of time that have not been discovered in the enviroment.

 

In a perfect world, we would have all of the servers in SCCM so we could just do an export and import it to UCMDB, but we don't have that option.

 

I have managed to get the current date and time to set for new CI's discovered, but getting it to set the attribute when a CI is rediscovered is proving to be difficult.

 

Can you think of a better and more effecient way of achieving aging without removing the asset system from the picture?

 

Cheers,

 

Tim

 

 

0 Likes
Highlighted
Absent Member.. jowillia Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

 

Hi Tim,

I can help with how you do this by modifying an OOB script. But, be warned , this is NOT the best option. These scripts can be overwritten by Content Packs, and you need to manage these changes well. The better way (and what I do these days after this one was overwritten and I learned the lesson) is to create a separate disocvery package - even for simple queries such as this.. having said that .. here is how you acheive populating a Windows CI custom attribute with a date derived from a wmi query by expanding the existing WMI_Connection_Utils.py script.

In my case the attribute name is 'osinstalldate'

Open WMI_Connection_Utils.py in your favourite editor. Save a backup.

 

1. Extend the wmi query in the doWMI function: so in your case add LocalDateTime to the Win32_OperatingSystem query as below (you may also want CurrentTimeZone?).

 

_wmiQuery = 'select Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,BuildNumber,Organization,RegisteredUser,TotalVisibleMemorySize,LastBootUpTime,OtherTypeDescription,description,InstallDate from Win32_OperatingSystem'
resultSet = client.executeQuery(_wmiQuery)

 

2. Add a similar lines as below to the same function where the resultSet is processed:

 

installdate = resultSet.getString(12)

hostOSH.setDateAttribute('osinstalldate', modeling.getDateFromUtcString(installdate))

 

Finally, remember the caveats at the top 🙂 It isn't the best approach to modify OOB scripts !

 

Absent Member.. Timtator Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

Thanks for taking the time to reply.

 

The code is not so much the problem anymore, as I ended up using the following code.

 

The problem I have is it only works the first time the CI is created.

 

I need to find where the best place to put the code would be, even if it is in another script that is called later.

 

3   import time
4   import datetime

349 Date_Time_String = time.strftime("%x")
350 Date_Time = datetime.datetime.strptime(Date_Time_String, '%m/%d/%y')
351 hostOSH.setAttribute('last_update_from_source', Date_Time)
352 vector.add(hostOSH)

 This then sets the current date and time on the last_update_from_source attribute, but only if the CI is being discovered for the first time. After that, the attribute is not updated again.

 

Is this because WMI is only used for discovering on the first attempt, then NTCMD is used from then on or something to this affect?

 

Thanks


Tim

0 Likes
Absent Member.. shameer_pm Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Enable the communication log. Add a logger.debug("Test Udpate") line after line 351 and rerun the job and see if the debug line is there in communication log. This way you can confirm first if the code is being executed in every run.
0 Likes
Absent Member.. jowillia Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi Tim

"Is this because WMI is only used for discovering on the first attempt, then NTCMD is used from then on"

 

Logically speaking, yes if the WMI script is only run once then the attribute will only update once. But I am not clear how you acheive the above. How does the CI drop out of the trigger for the WMI job and only appear in the trigger for the NTCMD job ? 

 

But anyway, I think the best way forward is to come up with a separate discovery job that just runs your custom code. Then you can more easily control the trigger TQL and the scheduling of the query. 

0 Likes
Absent Member.. PuneetS Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

Are you sure that the value of "Date_Time" variable is changing everytime you run the job?

Please include logger.debug to print the value of "Date_Time" and then run the job multiple times and check the communication log everytime to make sure that the value of "Date_Time" is changing.

 

And I agree with jowillia that the below statement is wrong:

"Is this because WMI is only used for discovering on the first attempt, then NTCMD is used from then on"

Regards
Puneet

If you find this reply helpful, Click the KUDOS button on the bottom to say 'Thanks'
Priyankasingh
New Member.

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

I am having the similar issue and I followed the same steps  to discover 'osinstalldate'

 

Steps:

 

Open WMI_Connection_Utils.py

 

1. Extend the wmi query in the doWMI function:

 

_wmiQuery = 'select Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,BuildNumber,Organization,RegisteredUser,TotalVisibleMemorySize,LastBootUpTime,OtherTypeDescription,description,InstallDate from Win32_OperatingSystem'
resultSet = client.executeQuery(_wmiQuery)

 

2. Added a similar lines as below to the same function where the resultSet is processed:

 

installdate = resultSet.getString(12)

hostOSH.setDateAttribute('osinstalldate', modeling.getDateFromUtcString(installdate))

 

3. Created attribute 'osinstalldate' in windows CIT.

 

4. Rerun the discovery.

 

But,  discovery is failing and in communication log i can see the osinstalldate in results but its not updating in the ci and getting error as "Error processing results of discovery adapters , details Attribute InstallDate".

 

Please help me to resolve this.

 

Thanks in Advance

Priyanka

0 Likes
Priyankasingh
New Member.

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

I had created 'osinstalldate' ad string. I was getting type mismatch error in communication log.So, created the attribute as date and it works.

 

Thanks

Priyanka

0 Likes
Priyankasingh
New Member.

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

I have recently created a custom date attribute on the Unix CI Type called "osinstalldate".

I need to modify the Unix discovery script to get the osinstalldate on the Unix CI and then updating the custom attribute.


I need to find out what code can be used to get the osinstall date and where to put and in which scipts(shellutils.py) script or another Scripts.

 

Please help me to resolve this.

 

Thanks in Advance
Priyanka

0 Likes
Absent Member.. jowillia Absent Member..
Absent Member..

Re: UCMDB - WMI_Connection_Util.py - Custom Attribute for current date\time

Hi,

 

It will depend on the OS type, but I do not know of any single command you can run on RedHat for example, that would give you the install date. The original package install dates may help, which you can find with rpm -qa..

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.