You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
5.5 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. import socket, platform, os, multiprocessing, json
  2. from datetime import timedelta
  3. from django.core import serializers
  4. from django.shortcuts import render_to_response
  5. from django.http import HttpResponseRedirect, HttpResponse
  6. from django.template import RequestContext
  7. from django.utils.translation import ugettext_lazy as _
  8. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  9. time_refresh = TIME_JS_REFRESH
  10. time_refresh_long = TIME_JS_REFRESH_LONG
  11. time_refresh_net = TIME_JS_REFRESH_NET
  12. version = VERSION
  13. def index(request):
  14. """
  15. Index page.
  16. """
  17. if not request.user.is_authenticated():
  18. return HttpResponseRedirect('/login')
  19. else:
  20. return HttpResponseRedirect('/main')
  21. def chunks(get, n):
  22. return [get[i:i+n] for i in range(0, len(get), n)]
  23. def get_uptime():
  24. """
  25. Get uptime
  26. """
  27. try:
  28. with open('/proc/uptime', 'r') as f:
  29. uptime_seconds = float(f.readline().split()[0])
  30. uptime_time = str(timedelta(seconds = uptime_seconds))
  31. data = uptime_time.split('.', 1)[0]
  32. except Exception,err:
  33. data = str(err)
  34. return data
  35. def get_hostname():
  36. """
  37. Get the hostname
  38. """
  39. try:
  40. data = socket.gethostname()
  41. except Exception,err:
  42. data = str(err)
  43. return data
  44. def get_ipaddress():
  45. """
  46. Get the IP Address
  47. """
  48. try:
  49. pipe = os.popen(" ip addr | grep -A3 'state UP' | awk '{printf \"%s,\",$2}'|awk -F, '{print $1, $2, $3}'")
  50. data = pipe.read().strip().split('\n')
  51. pipe.close()
  52. data = [i.split(None, 3) for i in data]
  53. for e in data:
  54. if len(e) > 3:
  55. itf = dict(zip([iter(e[0].strip(':'))]))
  56. else:
  57. itf = [e[0].strip(':')]
  58. ips = {'interface': itf, 'itfip': data}
  59. data = ips
  60. except Exception,err:
  61. data = str(err)
  62. return data
  63. def get_cpus():
  64. """
  65. Get the number of CPUs and model/type
  66. """
  67. try:
  68. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  69. data = pipe.read().strip().split(':')[-1]
  70. pipe.close()
  71. if not data:
  72. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  73. data = pipe.read().strip().split(':')[-1]
  74. pipe.close()
  75. cpus = multiprocessing.cpu_count()
  76. data = {'cpus': cpus, 'type': data}
  77. except Exception, err:
  78. data = str(err)
  79. return data
  80. def get_users():
  81. """
  82. Get the current logged in users
  83. """
  84. try:
  85. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  86. data = pipe.read().strip().split('\n')
  87. pipe.close()
  88. data = [i.split(None, 3) for i in data]
  89. except Exception, err:
  90. data = str(err)
  91. if data[0] == []:
  92. data = [['No', 'data', 'available']]
  93. return data
  94. def get_traffic(request):
  95. """
  96. Get the traffic for the specified interface
  97. """
  98. try:
  99. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  100. data = pipe.read().strip().split(':',1)[-1]
  101. pipe.close()
  102. if data == ' 0':
  103. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  104. data = pipe.read().strip().split(':',1)[-1]
  105. pipe.close()
  106. data = data.split()
  107. traffic_in = int(data[0])
  108. traffic_out = int(data[1])
  109. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  110. data = all_traffic
  111. except Exception,err:
  112. data = str(err)
  113. return data
  114. def get_platform():
  115. """
  116. Get the OS name
  117. """
  118. try:
  119. data = " ".join(platform.linux_distribution())
  120. except Exception,err:
  121. data = str(err)
  122. return data
  123. def get_disk():
  124. """
  125. Get disk usage
  126. """
  127. try:
  128. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  129. data = pipe.read().strip().split('\n')
  130. pipe.close()
  131. data = [i.split(None, 6) for i in data]
  132. except Exception,err:
  133. data = str(err)
  134. return data
  135. def get_mem():
  136. """
  137. Get memory usage
  138. """
  139. try:
  140. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  141. data = pipe.read().strip().split()
  142. pipe.close()
  143. allmem = int(data[0])
  144. freemem = int(data[1])
  145. percent = (100 - ((freemem * 100) / allmem))
  146. usage = (allmem - freemem)
  147. mem_usage = {'usage': usage, 'percent': percent}
  148. data = mem_usage
  149. except Exception,err:
  150. data = str(err)
  151. return data
  152. def get_cpu_usage():
  153. """
  154. Get the CPU usage and running processes
  155. """
  156. try:
  157. pipe = os.popen("ps aux --sort -%cpu,-rss")
  158. data = pipe.read().strip().split('\n')
  159. pipe.close()
  160. usage = [i.split(None, 10) for i in data]
  161. del usage[0]
  162. total_usage = []
  163. for element in usage:
  164. usage_cpu = element[2]
  165. total_usage.append(usage_cpu)
  166. total_usage = sum(float(i) for i in total_usage)
  167. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  168. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  169. data = cpu_used
  170. except Exception,err:
  171. data = str(err)
  172. return data
  173. def get_load():
  174. """
  175. Get load average
  176. """
  177. try:
  178. data = os.getloadavg()[0]
  179. except Exception, err:
  180. data = str(err)
  181. return data
  182. def getall(request):
  183. if not request.user.is_authenticated():
  184. return HttpResponseRedirect('/login')
  185. return render_to_response('main.html', {'gethostname': get_hostname(),
  186. 'getplatform': get_platform(),
  187. 'getcpus': get_cpus(),
  188. 'time_refresh': time_refresh,
  189. 'time_refresh_long': time_refresh_long,
  190. 'time_refresh_net': time_refresh_net,
  191. 'version': version
  192. }, context_instance=RequestContext(request))