0

I am working on a ceph mgr plugin. I have a dict that I get from self.get('health'). The keys and values in the dict changes depending on the health of the system.

What I want to achieve is to write a function in python that gets the value of self.get('health') every 1 minute and compare it with the previous value i.e. oldDictValue compared to newDictValue where newDictValue is the dict value gotten after 1 minute.

oldDictValue and newDictValue are variables holding values from self.get('health') at different times.

AHeyne
  • 3,377
  • 2
  • 11
  • 16
  • I think you can combine those two posts to solve your problem: [interval timer](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python) and [comparing 2 dictionaries](https://stackoverflow.com/questions/4527942/comparing-two-dictionaries-in-python) – Ha Bom Dec 29 '18 at 09:01
  • @HaBom this [https://stackoverflow.com/questions/4527942/comparing-two-dictionaries-in-python] will not quite work because the dict lenght will be different if there's a change in status – Kate Longchi Dec 29 '18 at 09:13
  • Yeah, there are so many ways to compare 2 dictionaries in that post, but they're just the ideas. Can you provide a sample of dictionaries? – Ha Bom Dec 29 '18 at 09:23
  • Can you put it in the question for easier understanding? – Ha Bom Dec 29 '18 at 09:25

1 Answers1

0

Here is a proposed pseudo code for what I wish to achieve. I am finding it difficult to covert this to a working solution

```  
   fun diff_health(old_health, new_health):
       old = {}
       new = {}
       updated = {}
      for code, item in new_health['checks'].iteritems():
          if code not in old_health['checks']:
              # it's a new alert
              new[code] = item
          else:
             old_item = old_health['checks'][code]
        if item['severity'] != old_item['severity'] or item['summary']['message']    != old_item['usmmary']['message']:
            # changed
            updated[code] = item
       for code, item in old_health['checks'].iteritems():
           if code not in new_health['checks']:
             # health alert has resolved
        ...
      return old, new, updated
```

new_health and old_health are values from self.get('heatth') at diffrent times