Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. Python implementation of a fan controller based on HDD
  2. and CPU temperature. This is a first draft implementation
  3. so do not expect the most clean code. It's a simple "it works"
  4. version.
  5.  
  6. Created by @marcusvb (GitLab & GitHub)
  7.  
  8. """
  9. from subprocess import Popen, PIPE, STDOUT
  10. import time
  11. import string
  12.  
  13. maxHddTemp = 53
  14. maxCpuTemp = 85
  15.  
  16. highHddTemp = 49
  17. highCpuTemp = 72
  18.  
  19. medHddTemp = 47
  20. medCpuTemp = 65
  21.  
  22. semiHddTemp = 46
  23. semiCpuTemp = 60
  24.  
  25. lowHddTemp = 45
  26. lowCpuTemp = 55
  27.  
  28. sleepTime = 5
  29. celcius = 'C'
  30. floatDot = '.'
  31. user = "123"
  32. password = "123"
  33. ip = "192.168.1.123"
  34.  
  35. #Do a command and return the stdout of proccess
  36. def sendcommand(cmdIn):
  37.    p = Popen(cmdIn, shell=True, executable="/bin/bash", stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
  38.    print(cmdIn)
  39.    return p.stdout.read()
  40.  
  41. #Do a ipmi command, setup for the default command.
  42. def ipmicmd(cmdIn):
  43.    return sendcommand("ipmitool -I lanplus -H " + ip +" -U " + user + " -P " + password + " " + cmdIn)
  44.  
  45. #Gets the CPU temperture from lm-sensors, returns the average of it.
  46. def getcputemp():
  47.    cmd = sendcommand('sensors  -u | grep "input"')
  48.    indexes = [pos for pos, char in enumerate(cmd) if char == floatDot]
  49.    cputemperatures = []
  50.    for loc in indexes:
  51.        temp = cmd[int(loc) - 2] + cmd[int(loc) - 1]
  52.        cputemperatures.append(int(temp))
  53.  
  54.    #return the average cpu temperature
  55.    return sum(cputemperatures) / int(len(cputemperatures))
  56.  
  57. #Check if controller was in automode, if so we override to manual.
  58. def checkstatus(status):
  59.    if (status == 4):
  60.        ipmicmd("raw 0x30 0x30 0x01 0x00")
  61.  
  62. #Main checking function which checks temperatures to the default set above.
  63. def checktemps(status):
  64.    avgCpuT = getcputemp()
  65.  
  66.    if (avgCpuT > maxCpuTemp):
  67.        if (status != 4):
  68.            ipmicmd("raw 0x30 0x30 0x01 0x01")
  69.            print("Setting to auto/loud mode, Server it too hot")
  70.        status = 4
  71.  
  72.    elif(avgCpuT > highCpuTemp):
  73.        if (status != 3):
  74.            checkstatus(status)
  75.            ipmicmd("raw 0x30 0x30 0x02 0xff 0x14")
  76.            print("Setting to 5000RPM, Server is hot")
  77.        status = 3
  78.  
  79.    elif(avgCpuT > medCpuTemp):
  80.        if (status != 2):
  81.            checkstatus(status)
  82.            ipmicmd("raw 0x30 0x30 0x02 0xff 0x08")
  83.            print("Setting to 3600RPM, Server is toasty")
  84.        status = 2
  85.  
  86.    elif(avgCpuT > semiCpuTemp):
  87.        if (status != 1):
  88.            checkstatus(status)
  89.            ipmicmd("raw 0x30 0x30 0x02 0xff 0x04")
  90.            print("Setting to 3200RPM, Server is semi")
  91.        status = 1
  92.  
  93.    else:
  94.        checkstatus(status)
  95.        if (status != 0):
  96.            ipmicmd("raw 0x30 0x30 0x02 0xff 0x02")
  97.            print("Setting to minimum RPM, Server is cool")
  98.        status = 0
  99.  
  100.    print("Cpu at: " + str(avgCpuT) + " celcius \n")
  101.    return status
  102.  
  103. #Main running function.
  104. def main():
  105.    ipmicmd("raw 0x30 0x30 0x01 0x00")
  106.    status = 999
  107.    while True:
  108.        time.sleep(sleepTime)
  109.        status = checktemps(status)
  110.        print("Sleeping for " + str(sleepTime))
  111. if __name__ == '__main__':
  112.    main()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement