Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. """
  2. Python fan controller for Dell R510 based on CPU temperature.
  3. """
  4. from subprocess import Popen, PIPE, STDOUT
  5. import time
  6. import string
  7.  
  8. Tcase = 68 # Tcase, maximum temperature allowed at the processor Integrated Heat Spreader (IHS).
  9.  
  10. sleepTime = 10
  11. celcius = 'C'
  12. floatDot = '.'
  13. user = "root"
  14. password = "calvin"
  15. ip = "192.168.x.xx"
  16.  
  17. #Do a command and return the stdout of proccess
  18. def sendcommand(cmdIn):
  19. p = Popen(cmdIn, shell=True, executable="/bin/bash", stdin=PIPE, stdout=PIPE, universal_newlines = True, stderr=STDOUT, close_fds=True)
  20. return p.stdout.read()
  21.  
  22. #Do a ipmi command, setup for the default command.
  23. def ipmicmd(cmdIn):
  24. return sendcommand("ipmitool " + cmdIn)
  25.  
  26. #Gets the CPU tempertures from lm-sensors, returns the maximum.
  27. def getcputemp():
  28. cmd = sendcommand('sensors -u | grep "input"')
  29. indexes = [pos for pos, char in enumerate(cmd) if char == floatDot]
  30. cputemperatures = []
  31. for loc in indexes:
  32. temp = cmd[int(loc) - 2] + cmd[int(loc) - 1]
  33. cputemperatures.append(int(temp))
  34.  
  35. #return the maximum cpu temperature
  36. return max(cputemperatures)
  37.  
  38. #return the average cpu temperature
  39. #return sum(cputemperatures) / int(len(cputemperatures))
  40.  
  41. #Check if controller was in automode, if so we override to manual.
  42. def checkstatus(status):
  43. if (status == 5):
  44. ipmicmd("raw 0x30 0x30 0x01 0x00")
  45.  
  46. #Main checking function which checks temperatures to the default set above.
  47. def checktemps(status):
  48. maxCpuT = getcputemp()
  49.  
  50. if (maxCpuT <= (Tcase - 20)):
  51. if (status != 1):
  52. checkstatus(status)
  53. ipmicmd("raw 0x30 0x30 0x02 0xff 0x10")
  54. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to low RPM", flush=True)
  55. status = 1
  56.  
  57. elif (maxCpuT <= (Tcase - 9)):
  58. if (status != 1):
  59. checkstatus(status)
  60. ipmicmd("raw 0x30 0x30 0x02 0xff 0x18")
  61. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to 4440 RPM", flush=True)
  62. status = 2
  63.  
  64. elif(maxCpuT > (Tcase - 9) and maxCpuT <= (Tcase -7)):
  65. if (status != 2):
  66. checkstatus(status)
  67. ipmicmd("raw 0x30 0x30 0x02 0xff 0x26")
  68. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to 7200 RPM", flush=True)
  69. status = 3
  70.  
  71. elif(maxCpuT > (Tcase - 7) and maxCpuT <= (Tcase -5)):
  72. if (status != 3):
  73. checkstatus(status)
  74. ipmicmd("raw 0x30 0x30 0x02 0xff 0x34")
  75. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to 7920 RPM", flush=True)
  76. status = 4
  77.  
  78. elif(maxCpuT > (Tcase - 5) and maxCpuT <= (Tcase -2)):
  79. if (status != 4):
  80. checkstatus(status)
  81. ipmicmd("raw 0x30 0x30 0x02 0xff 0x36")
  82. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to 10320 RPM", flush=True)
  83. status = 5
  84.  
  85. else:
  86. if (status != 5):
  87. ipmicmd("raw 0x30 0x30 0x01 0x01")
  88. print("Cpu at: " + str(maxCpuT) + " celcius, Fan set to auto/loud mode, Server it too hot")
  89. status = 5
  90.  
  91. # print("Cpu at: " + str(maxCpuT) + " celcius, Fan status =" + str(status),flush=True)
  92. return status
  93.  
  94. #Main running function.
  95. def main():
  96. status = 5
  97. while True:
  98. time.sleep(sleepTime)
  99. status = checktemps(status)
  100. #print("Sleeping for " + str(sleepTime))
  101. if __name__ == '__main__':
  102. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement