Add structured logging for threshold events

Add capability to create structured logs for threshold crossing
(compatible with Redfish Sensor Events) along with ability to
resolve the logs when the threshold event recovers.

Tested: Created an inventory with an external sensor named
`HostDevTemp` and a virtual sensor named `SPECIAL_SENSOR` configured
to consumes this sensor which has set thresholds
`"HardShutdownHigh": 50` and `"CriticalHigh": 35`. This would allow
us to change the sensor value and watch the service handling the
threshold crossing.

1. Change sensor value to 40+ to observe upper critical threshold
   handling.
```
root@bmc:~# busctl introspect -l xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/3 xyz.openbmc_project.Logging.Entry | grep "AdditionalData\|Message\|Resolved"
.AdditionalData property  a{ss} 8 "READING_VALUE" "41.0" "SENSOR_NAME" "/xyz/openbmc_project/sensors/temperature/SPECIAL_SENSOR" "THRESHOLD_VALUE" "35.0" "UNITS" "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" <snip>
.Message property  s "xyz.openbmc_project.Sensor.Threshold.ReadingAboveUpperCriticalThreshold"
.Resolved property b false
```

2. change sensor value to 70+ to observe upper hard-shutdown threshold
   crossing.
```
root@bmc:~# busctl introspect -l xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/5 xyz.openbmc_project.Logging.Entry | grep "AdditionalData\|Message\|Resolved"
.AdditionalData property  a{ss} 8 "READING_VALUE" "71.0" "SENSOR_NAME" "/xyz/openbmc_project/sensors/temperature/SPECIAL_SENSOR" "THRESHOLD_VALUE" "50.0" "UNITS" "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" <snip>
.Message property  s "xyz.openbmc_project.Sensor.Threshold.ReadingAboveUpperHardShutdownThreshold"
.Resolved property  b false
```

3. Change sensor value back to 40+. Ensure upper hard-shutdown threshold
   is marked as resolved.
```
root@bmc:~# busctl introspect -l xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/5 xyz.openbmc_project.Logging.Entry | grep "AdditionalData\|Message\|Resolved"
.AdditionalData property  a{ss}     8 "READING_VALUE" "71.0" "SENSOR_NAME" "/xyz/openbmc_project/sensors/temperature/SPECIAL_SENSOR" "THRESHOLD_VALUE" "50.0" "UNITS" "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" <snip>
.Message property  s         "xyz.openbmc_project.Sensor.Threshold.ReadingAboveUpperHardShutdownThreshold"
.Resolved property  b true
```

4. Change sensor value to 1+. Ensure that the upper critical threshold
   is marked as resolved. And a new event log is created for the
   sensors stating that the sensor is now in normal range.
```
root@bmc:~# busctl introspect -l xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/3 xyz.openbmc_project.Logging.Entry | grep "AdditionalData\|Message\|Resolved"
.AdditionalData property  a{ss} 8 "READING_VALUE" "41.0" "SENSOR_NAME" "/xyz/openbmc_project/sensors/temperature/SPECIAL_SENSOR" "THRESHOLD_VALUE" "35.0" "UNITS" "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" <snip>
.Message property  s "xyz.openbmc_project.Sensor.Threshold.ReadingAboveUpperCriticalThreshold"
.Resolved property  b  true
```
```
root@bmc:~# busctl introspect -l xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/6 xyz.openbmc_project.Logging.Entry | grep "AdditionalData\|Message\|Resolved"
.AdditionalData property  a{ss} 7 "READING_VALUE" "2.0" "SENSOR_NAME" "/xyz/openbmc_project/sensors/temperature/SPECIAL_SENSOR" "UNITS" "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" <snip>
.Message property  s  "xyz.openbmc_project.Sensor.Threshold.SensorReadingNormalRange"
.Resolved property  b false
```

Change-Id: I660b6a477af38c9314680ce12705c460bae23fd8
Signed-off-by: Amithash Prasad <amithash@meta.com>
3 files changed
tree: 93c9f4c4e1ac42bddfb02f80ad6e320faf98a985
  1. subprojects/
  2. test/
  3. .clang-format
  4. .clang-tidy
  5. .editorconfig
  6. .gitignore
  7. .prettierrc.yaml
  8. calculate.cpp
  9. calculate.hpp
  10. dbusSensor.cpp
  11. dbusSensor.hpp
  12. dbusUtils.cpp
  13. dbusUtils.hpp
  14. exprtkTools.hpp
  15. LICENSE
  16. main.cpp
  17. meson.build
  18. meson.options
  19. OWNERS
  20. phosphor-virtual-sensor.service.in
  21. README.md
  22. thresholds.hpp
  23. virtual_sensor_config.json
  24. virtualSensor.cpp
  25. virtualSensor.hpp
README.md

phosphor-virtual-sensor

phosphor-virtual-sensor reads the configuration file virtual_sensor_config.json from one of three locations:

  1. The current directory.
  2. /var/lib/phosphor-virtual-sensor
  3. /usr/share/phosphor-virtual-sensor

By default the repository will install a sample config into (3).

There are two types of data in this file.

virtual sensor configuration information

See virtual_sensor_config.json in this repository for an example. Sensors added this way can use any expression that is accepted by exprtk.

information to get a virtual sensor configuration from D-Bus

For example:

{
  "Desc": {
    "Config": "D-Bus"
  }
}

Sensors added this way can only use a set of restricted calculations. Currently supported types are Average, Maximum, Minimum, Sum and ModifiedMedian.

The virtual sensor configuration information needs to be added into the relevant hardware configuration file in entity-manager. This method of adding a virtual sensor allows a recipe that builds for different hardware configurations to have different virtual sensors for each configuration.

The virtual sensor configuration in entity manager follows a different format to the JSON in virtual_sensor_config.json (specified in entity-manager/schemas/VirtualSensor.json).