UCMDB and UD Practitioners Forum (Previously CMS)
cancel

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

Highlighted
Timtator
Occasional Advisor

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

 

 

 

17 REPLIES
Dima Gomel
HPE Expert

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
If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
Click the Like button at the bottom to say 'Thanks'.
Timtator
Occasional Advisor

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

 

 

jowillia
Regular Collector

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 !

 

Timtator
Occasional Advisor

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

shameer_pm
Super Collector

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.
jowillia
Regular Collector

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. 

PuneetS
Frequent Visitor

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
Acclaimed Contributor

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

Priyankasingh
Acclaimed Contributor

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

Priyankasingh
Acclaimed Contributor

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

jowillia
Regular Collector

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..

 

 

Priyankasingh
Acclaimed Contributor

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

Hi,

 

For RHEL unix servers, I got the command to get osinstall date

 

rpm -qi basesystem | grep -i "Install Date"

 

 

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

 

So, Please tell me which scipts needs to be modified to discover osinstalldate.

 

Thanks

Priyanka

 

jowillia
Regular Collector

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

I would suggest you write a custom script and package it separately. As I mentioined in the first post, modifying OOB scripts is not a great idea.

The package you need to create can then also be used for the next custom attribute you decide you want. 

Dima Gomel
HPE Expert

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

To elaborate on previous post.

When we saying that changing of OOTB script isn't a good practice, we are not meaning you can't implement what ever you want. You can and system sypporting it.

HP takes OOTB files as our zone of responsibilities. They will be owerride with every CP update you will install. So,  If you need to implement a change to OOTB job, do a copy, call it CustomerX_WMI_Connection_Util.py. This will presave results of your work.

Remember, that you could raise enhancement request for the product. If the request is not unique for other customers, it will be implemented by HP.

 

As a general guidelines for debugging of created code.

1. Do the change in the script.

2. Retrive  a communication log with results. Check that results contains information that need to be populated as value for appropriate attribute on expected CIT. (it's in XML form at the end of the script).

3. Go to server and check mam.autodicovery.audit.log. Every data-in operation (AKA bulk in case of dicovery) will have two strings of printouts. search by Job name/timestamp. The fisrt string will show what you sending from the script, the second will either show success of reconciliation of sended data, or error.

4.  In case of error, please check reconciliation.log  for specific problem. It could be needed to be moved to DEBUG in /UCMDBServer/conf/log/cmdb.reconciliation.properties.

 

Be also aware about redundancy filter on the probe side. If you are on UCMDB 9, be sure that the attribute you sending will have Probe_Side_ID qualifier on the class model. In UCMDB 10 all attributes are monitored for the change in redundancy filter.

 

Hope this helps.

Regards
-Dmitry Gomel, PMP
If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
Click the Like button at the bottom to say 'Thanks'.
PriyankaSingh2
Acclaimed Contributor

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

Hi Dima,

 

To Disocover osinstalldate for unix, I have modified the TTY_Connection_Utils.py script.

 

But in communication log, getting the error as 

 

AttributeError: class HostDataObject has no attribute 'ATTRIBUTE_OS_INSTALLDATE'

 

Please help me to resolve this.

 

 

Thanks

Priyanka

 

 

 

Dima Gomel
HPE Expert

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

Is 'ATTRIBUTE_OS_INSTALLDATE' a new attribute you're willing to populate?

If yes, was it added to the class model?

Regards
-Dmitry Gomel, PMP
If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
Click the Like button at the bottom to say 'Thanks'.
PriyankaSingh2
Acclaimed Contributor

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

Hi,

 

I have added OS_INSTALLDATE attribute in class model and it works.

 

Thanks for the help.

 

Priyanka

//Add this to "OnDomLoad" event