Advertisement
FranzVuttke

sensors.py

Nov 19th, 2023 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | Source Code | 0 0
  1. #!/usr/bin/python
  2.  
  3. #
  4. # by OuNiS 11.2023
  5. #
  6.  
  7. # https://pastebin.com/gq9VmYZd
  8.  
  9. '''
  10. .json file structure
  11. {
  12.   "coretemp-isa-0000":{
  13.      "Adapter": "ISA adapter",
  14.      "Package id 0":{
  15.         "temp1_input": 36.000,
  16.         "temp1_max": 80.000,
  17.         "temp1_crit": 100.000,
  18.         "temp1_crit_alarm": 0.000
  19.      },
  20.      "Core 0":{
  21.         "temp2_input": 36.000,
  22.         "temp2_max": 80.000,
  23.         "temp2_crit": 100.000,
  24.         "temp2_crit_alarm": 0.000
  25.      },
  26.      "Core 1":{
  27.         "temp3_input": 35.000,
  28.         "temp3_max": 80.000,
  29.         "temp3_crit": 100.000,
  30.         "temp3_crit_alarm": 0.000
  31.      }
  32.   }
  33. }
  34.  
  35. '''
  36.  
  37.  
  38.  
  39. import json
  40. import os
  41. import pathlib
  42. import sys
  43.  
  44. MAIN_FILE_NAME =sys.argv[0].split("/")[-1]
  45. SENSORS_JSON_FILE = ".".join([MAIN_FILE_NAME,"json~"])
  46. # print(SENSORS_JSON_FILE)
  47.  
  48. # uruchomienie sensors, wynik w json zapisany do pliku
  49. os.system("sensors -A -j > {}".format(SENSORS_JSON_FILE))
  50. print("Czas pomiaru: ")
  51. # os.system("~/batch/script_header")
  52. os.system("date +\"%Y-%m-%d %H:%M:%S\"")
  53.  
  54. lines = []
  55. with open(SENSORS_JSON_FILE) as file:
  56.     lines = file.readlines()
  57.  
  58.  
  59. sensj = json.loads("".join(lines))
  60.  
  61. print("Keys: ")
  62. #for k in sensj.keys():
  63. #    print(k)
  64.  
  65. for k in sensj.get("coretemp-isa-0000") .keys():
  66.     print(k)
  67.  
  68. CPU_TEMP = sensj.get("coretemp-isa-0000").get("Package id 0").get("temp1_input")
  69. CORE_0_TEMP = sensj.get("coretemp-isa-0000").get("Core 0").get("temp2_input")
  70. CORE_1_TEMP = sensj.get("coretemp-isa-0000").get("Core 1").get("temp3_input")
  71. print(f'CPU temp: {CPU_TEMP}')
  72. print(f'\tCore 0 temp: {CORE_0_TEMP}')
  73. print(f'\tCore 1 temp: {CORE_1_TEMP}')
  74.  
  75. # Cleaning...
  76. pathlib.Path.unlink(SENSORS_JSON_FILE, missing_ok=True)
  77. # try:
  78. #     # try to erase temp file...
  79. #
  80. #     pathlib.Path.unlink(SENSORS_JSON_FILE)
  81. # except Exception as err:
  82. #     # maybe file disappeard by yourself...
  83. #     # well, do nothing...
  84. #     ...
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement