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.

313 lines
7.3 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. #The MIT License (MIT)
  2. #
  3. #Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/
  4. #
  5. #Permission is hereby granted, free of charge, to any person obtaining a copy
  6. #of this software and associated documentation files (the "Software"), to deal
  7. #in the Software without restriction, including without limitation the rights
  8. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. #copies of the Software, and to permit persons to whom the Software is
  10. #furnished to do so, subject to the following conditions:
  11. #
  12. #The above copyright notice and this permission notice shall be included in all
  13. #copies or substantial portions of the Software.
  14. #
  15. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. #SOFTWARE.
  22. import socket, platform, os, multiprocessing, json
  23. from datetime import timedelta
  24. from django.contrib.auth.decorators import login_required
  25. from django.core import serializers
  26. from django.shortcuts import render_to_response
  27. from django.http import HttpResponseRedirect, HttpResponse
  28. from django.template import RequestContext
  29. from django.utils.translation import ugettext_lazy as _
  30. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  31. time_refresh = TIME_JS_REFRESH
  32. time_refresh_long = TIME_JS_REFRESH_LONG
  33. time_refresh_net = TIME_JS_REFRESH_NET
  34. version = VERSION
  35. @login_required(login_url='/login/')
  36. def index(request):
  37. """
  38. Index page.
  39. """
  40. return HttpResponseRedirect('/main')
  41. def chunks(get, n):
  42. return [get[i:i+n] for i in range(0, len(get), n)]
  43. def get_uptime():
  44. """
  45. Get uptime
  46. """
  47. try:
  48. with open('/proc/uptime', 'r') as f:
  49. uptime_seconds = float(f.readline().split()[0])
  50. uptime_time = str(timedelta(seconds = uptime_seconds))
  51. data = uptime_time.split('.', 1)[0]
  52. except Exception,err:
  53. data = str(err)
  54. return data
  55. def get_hostname():
  56. """
  57. Get the hostname
  58. """
  59. try:
  60. data = socket.gethostname()
  61. except Exception,err:
  62. data = str(err)
  63. return data
  64. def get_ipaddress():
  65. """
  66. Get the IP Address
  67. """
  68. try:
  69. pipe = os.popen(" ip addr | grep -A3 'state UP' | awk '{printf \"%s,\",$2}'|awk -F, '{print $1, $2, $3}'")
  70. data = pipe.read().strip().split('\n')
  71. pipe.close()
  72. data = [i.split(None, 3) for i in data]
  73. for e in data:
  74. if len(e) > 3:
  75. itf = dict(zip([iter(e[0].strip(':'))]))
  76. else:
  77. itf = [e[0].strip(':')]
  78. ips = {'interface': itf, 'itfip': data}
  79. data = ips
  80. except Exception,err:
  81. data = str(err)
  82. return data
  83. def get_cpus():
  84. """
  85. Get the number of CPUs and model/type
  86. """
  87. try:
  88. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  89. data = pipe.read().strip().split(':')[-1]
  90. pipe.close()
  91. if not data:
  92. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  93. data = pipe.read().strip().split(':')[-1]
  94. pipe.close()
  95. cpus = multiprocessing.cpu_count()
  96. data = {'cpus': cpus, 'type': data}
  97. except Exception, err:
  98. data = str(err)
  99. return data
  100. def get_users():
  101. """
  102. Get the current logged in users
  103. """
  104. try:
  105. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  106. data = pipe.read().strip().split('\n')
  107. pipe.close()
  108. data = [i.split(None, 3) for i in data]
  109. except Exception, err:
  110. data = str(err)
  111. if data[0] == []:
  112. data = [['No', 'data', 'available']]
  113. return data
  114. def get_traffic(request):
  115. """
  116. Get the traffic for the specified interface
  117. """
  118. try:
  119. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  120. data = pipe.read().strip().split(':',1)[-1]
  121. pipe.close()
  122. if data == ' 0':
  123. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  124. data = pipe.read().strip().split(':',1)[-1]
  125. pipe.close()
  126. data = data.split()
  127. traffic_in = int(data[0])
  128. traffic_out = int(data[1])
  129. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  130. data = all_traffic
  131. except Exception,err:
  132. data = str(err)
  133. return data
  134. def get_platform():
  135. """
  136. Get the OS name
  137. """
  138. try:
  139. data = " ".join(platform.linux_distribution())
  140. except Exception,err:
  141. data = str(err)
  142. return data
  143. def get_disk():
  144. """
  145. Get disk usage
  146. """
  147. try:
  148. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  149. data = pipe.read().strip().split('\n')
  150. pipe.close()
  151. data = [i.split(None, 6) for i in data]
  152. except Exception,err:
  153. data = str(err)
  154. return data
  155. def get_disk_rw():
  156. """
  157. Get the disk reads and writes
  158. """
  159. try:
  160. pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'")
  161. data = pipe.read().strip().split('\n')
  162. pipe.close()
  163. rws = []
  164. for i in data:
  165. if i.isalpha():
  166. pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'")
  167. rw = pipe.read().strip().split()
  168. pipe.close()
  169. rws.append([i, rw[0], rw[1]])
  170. if not rws:
  171. pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'")
  172. rw = pipe.read().strip().split()
  173. pipe.close()
  174. rws.append([data[0], rw[0], rw[1]])
  175. data = rws
  176. except Exception,err:
  177. data = str(err)
  178. return data
  179. def get_mem():
  180. """
  181. Get memory usage
  182. """
  183. try:
  184. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  185. data = pipe.read().strip().split()
  186. pipe.close()
  187. allmem = int(data[0])
  188. freemem = int(data[1])
  189. percent = (100 - ((freemem * 100) / allmem))
  190. usage = (allmem - freemem)
  191. mem_usage = {'usage': usage, 'percent': percent}
  192. data = mem_usage
  193. except Exception,err:
  194. data = str(err)
  195. return data
  196. def get_cpu_usage():
  197. """
  198. Get the CPU usage and running processes
  199. """
  200. try:
  201. pipe = os.popen("ps aux --sort -%cpu,-rss")
  202. data = pipe.read().strip().split('\n')
  203. pipe.close()
  204. usage = [i.split(None, 10) for i in data]
  205. del usage[0]
  206. total_usage = []
  207. for element in usage:
  208. usage_cpu = element[2]
  209. total_usage.append(usage_cpu)
  210. total_usage = sum(float(i) for i in total_usage)
  211. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  212. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  213. data = cpu_used
  214. except Exception,err:
  215. data = str(err)
  216. return data
  217. def get_load():
  218. """
  219. Get load average
  220. """
  221. try:
  222. data = os.getloadavg()[0]
  223. except Exception, err:
  224. data = str(err)
  225. return data
  226. @login_required(login_url='/login/')
  227. def getall(request):
  228. return render_to_response('main.html', {'gethostname': get_hostname(),
  229. 'getplatform': get_platform(),
  230. 'getcpus': get_cpus(),
  231. 'getdiskrw': get_disk_rw(),
  232. 'time_refresh': time_refresh,
  233. 'time_refresh_long': time_refresh_long,
  234. 'time_refresh_net': time_refresh_net,
  235. 'version': version
  236. }, context_instance=RequestContext(request))