|
| 1 | +#! /usr/bin/env python3 |
| 2 | +"""Hard Drive (or any other memory) monitor. Contains a the monitor node and its main function.""" |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# |
| 5 | +# Software License Agreement (BSD License) |
| 6 | +# |
| 7 | +# Copyright (c) 2009, Willow Garage, Inc. |
| 8 | +# All rights reserved. |
| 9 | +# |
| 10 | +# Redistribution and use in source and binary forms, with or without |
| 11 | +# modification, are permitted provided that the following conditions |
| 12 | +# are met: |
| 13 | +# |
| 14 | +# * Redistributions of source code must retain the above copyright |
| 15 | +# notice, this list of conditions and the following disclaimer. |
| 16 | +# * Redistributions in binary form must reproduce the above |
| 17 | +# copyright notice, this list of conditions and the following |
| 18 | +# disclaimer in the documentation and/or other materials provided |
| 19 | +# with the distribution. |
| 20 | +# * Neither the name of the Willow Garage nor the names of its |
| 21 | +# contributors may be used to endorse or promote products derived |
| 22 | +# from this software without specific prior written permission. |
| 23 | +# |
| 24 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | +# POSSIBILITY OF SUCH DAMAGE. |
| 36 | + |
| 37 | +# \author Kevin Watts |
| 38 | +# \author Antoine Lima |
| 39 | + |
| 40 | +from pathlib import Path |
| 41 | +from shutil import disk_usage |
| 42 | +from socket import gethostname |
| 43 | +from typing import List |
| 44 | + |
| 45 | +from diagnostic_msgs.msg import DiagnosticStatus, KeyValue |
| 46 | +from diagnostic_updater import Updater |
| 47 | +from rcl_interfaces.msg import SetParametersResult |
| 48 | +import rclpy |
| 49 | +from rclpy.node import Node |
| 50 | + |
| 51 | + |
| 52 | +FREE_PERCENT_LOW = 0.05 |
| 53 | +FREE_PERCENT_CRIT = 0.01 |
| 54 | +DICT_STATUS = { |
| 55 | + DiagnosticStatus.OK: 'OK', |
| 56 | + DiagnosticStatus.WARN: 'Warning', |
| 57 | + DiagnosticStatus.ERROR: 'Error', |
| 58 | +} |
| 59 | +DICT_USAGE = { |
| 60 | + DiagnosticStatus.OK: 'OK', |
| 61 | + DiagnosticStatus.WARN: 'Low Disk Space', |
| 62 | + DiagnosticStatus.ERROR: 'Very Low Disk Space', |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +class HDMonitor(Node): |
| 67 | + """Diagnostic node checking the remaining space on the specified hard drive. |
| 68 | +
|
| 69 | + Three ROS parameters: |
| 70 | + - path: Path on the filesystem to check (string, default: home directory) |
| 71 | + - free_percent_low: Percentage at which to consider the space left as low |
| 72 | + - free_percent_crit: Percentage at which to consider the space left as critical |
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self): |
| 76 | + hostname = gethostname().replace('.', '_').replace('-', '_') |
| 77 | + super().__init__(f'hd_monitor_{hostname}') |
| 78 | + |
| 79 | + self._path = '~' |
| 80 | + self._free_percent_low = 0.05 |
| 81 | + self._free_percent_crit = 0.01 |
| 82 | + |
| 83 | + self.add_on_set_parameters_callback(self.callback_config) |
| 84 | + self.declare_parameter('path', self._path) |
| 85 | + self.declare_parameter('free_percent_low', self._free_percent_low) |
| 86 | + self.declare_parameter('free_percent_crit', self._free_percent_crit) |
| 87 | + |
| 88 | + self._updater = Updater(self) |
| 89 | + self._updater.setHardwareID(hostname) |
| 90 | + self._updater.add(f'{hostname} HD Usage', self.check_disk_usage) |
| 91 | + |
| 92 | + def callback_config(self, params: List[rclpy.Parameter]): |
| 93 | + """Retrieve ROS parameters. |
| 94 | +
|
| 95 | + see the class documentation for declared parameters. |
| 96 | + """ |
| 97 | + for param in params: |
| 98 | + match param.name: |
| 99 | + case 'path': |
| 100 | + self._path = str( |
| 101 | + Path(param.value).expanduser().resolve(strict=True) |
| 102 | + ) |
| 103 | + case 'free_percent_low': |
| 104 | + self._free_percent_low = param.value |
| 105 | + case 'free_percent_crit': |
| 106 | + self._free_percent_crit = param.value |
| 107 | + |
| 108 | + return SetParametersResult(successful=True) |
| 109 | + |
| 110 | + def check_disk_usage(self, diag: DiagnosticStatus) -> DiagnosticStatus: |
| 111 | + """Compute the disk usage and derive a status from it. |
| 112 | +
|
| 113 | + Task periodically ran by the diagnostic updater. |
| 114 | + """ |
| 115 | + diag.level = DiagnosticStatus.OK |
| 116 | + |
| 117 | + total, _, free = disk_usage(self._path) |
| 118 | + percent = free / total |
| 119 | + |
| 120 | + if percent > self._free_percent_low: |
| 121 | + diag.level = DiagnosticStatus.OK |
| 122 | + elif percent > self._free_percent_crit: |
| 123 | + diag.level = DiagnosticStatus.WARN |
| 124 | + else: |
| 125 | + diag.level = DiagnosticStatus.ERROR |
| 126 | + |
| 127 | + total_go = total // (1024 * 1024) |
| 128 | + diag.values.extend( |
| 129 | + [ |
| 130 | + KeyValue(key='Name', value=self._path), |
| 131 | + KeyValue(key='Status', value=DICT_STATUS[diag.level]), |
| 132 | + KeyValue(key='Total (Go)', value=str(total_go)), |
| 133 | + KeyValue(key='Available (%)', value=str(round(percent, 2))), |
| 134 | + ] |
| 135 | + ) |
| 136 | + |
| 137 | + diag.message = DICT_USAGE[diag.level] |
| 138 | + return diag |
| 139 | + |
| 140 | + |
| 141 | +def main(args=None): |
| 142 | + """Run the HDMonitor class.""" |
| 143 | + rclpy.init(args=args) |
| 144 | + |
| 145 | + node = HDMonitor() |
| 146 | + try: |
| 147 | + rclpy.spin(node) |
| 148 | + except KeyboardInterrupt: |
| 149 | + pass |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == '__main__': |
| 153 | + main() |
0 commit comments