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.

649 lines
18 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
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 json
  23. from django.contrib.auth.decorators import login_required
  24. from django.shortcuts import render_to_response
  25. from django.core.urlresolvers import reverse, reverse_lazy
  26. from django.http import HttpResponse
  27. from django.template import RequestContext
  28. from settings import (TIME_JS_REFRESH, TIME_JS_REFRESH_LONG,
  29. TIME_JS_REFRESH_NET, _VERSION)
  30. import services
  31. import urls
  32. def get_pydash_urls():
  33. results = dict()
  34. for url_pattern in urls.urlpatterns:
  35. name = url_pattern.name
  36. try:
  37. # Works for non regex patterns
  38. results[name] = reverse(name)
  39. except Exception:
  40. # Get relative url prefix of regex patterns
  41. # Hacky solution but it works.
  42. # E.g. reverse('platform', args=[0])[:-2] -> /info/platform/
  43. # reverse('getcpus', args[0])[:-2] -> /info/getcpus/
  44. results[name] = reverse(name, args=[0])[:-2]
  45. return results
  46. @login_required(login_url=reverse_lazy('login'))
  47. def index(request):
  48. """
  49. Index page.
  50. """
  51. pydash_urls = get_pydash_urls()
  52. return render_to_response('main.html', {'time_refresh': TIME_JS_REFRESH,
  53. 'time_refresh_long': TIME_JS_REFRESH_LONG,
  54. 'time_refresh_net': TIME_JS_REFRESH_NET,
  55. 'version': _VERSION,
  56. 'pydashUrls': pydash_urls},
  57. context_instance=RequestContext(request))
  58. @login_required(login_url=reverse_lazy('login'))
  59. def getnetstat(request):
  60. """
  61. Return netstat output
  62. """
  63. try:
  64. net_stat = services.get_netstat()
  65. except Exception:
  66. net_stat = None
  67. data = json.dumps(net_stat)
  68. response = HttpResponse()
  69. response['Content-Type'] = "text/javascript"
  70. response.write(data)
  71. return response
  72. @login_required(login_url=reverse_lazy('login'))
  73. def platform(request, name):
  74. """
  75. Return the hostname
  76. """
  77. getplatform = services.get_platform()
  78. hostname = getplatform['hostname']
  79. osname = getplatform['osname']
  80. kernel = getplatform['kernel']
  81. data = {}
  82. if name == 'hostname':
  83. try:
  84. data = hostname
  85. except Exception:
  86. data = None
  87. if name == 'osname':
  88. try:
  89. data = osname
  90. except Exception:
  91. data = None
  92. if name == 'kernel':
  93. try:
  94. data = kernel
  95. except Exception:
  96. data = None
  97. data = json.dumps(data)
  98. response = HttpResponse()
  99. response['Content-Type'] = "text/javascript"
  100. response.write(data)
  101. return response
  102. @login_required(login_url=reverse_lazy('login'))
  103. def getcpus(request, name):
  104. """
  105. Return the CPU number and type/model
  106. """
  107. cpus = services.get_cpus()
  108. cputype = cpus['type']
  109. cpucount = cpus['cpus']
  110. data = {}
  111. if name == 'type':
  112. try:
  113. data = cputype
  114. except Exception:
  115. data = None
  116. if name == 'count':
  117. try:
  118. data = cpucount
  119. except Exception:
  120. data = None
  121. data = json.dumps(data)
  122. response = HttpResponse()
  123. response['Content-Type'] = "text/javascript"
  124. response.write(data)
  125. return response
  126. @login_required(login_url=reverse_lazy('login'))
  127. def uptime(request):
  128. """
  129. Return uptime
  130. """
  131. try:
  132. up_time = services.get_uptime()
  133. except Exception:
  134. up_time = None
  135. data = json.dumps(up_time)
  136. response = HttpResponse()
  137. response['Content-Type'] = "text/javascript"
  138. response.write(data)
  139. return response
  140. @login_required(login_url=reverse_lazy('login'))
  141. def getdisk(request):
  142. """
  143. Return the disk usage
  144. """
  145. try:
  146. diskusage = services.get_disk()
  147. except Exception:
  148. diskusage = None
  149. data = json.dumps(diskusage)
  150. response = HttpResponse()
  151. response['Content-Type'] = "text/javascript"
  152. response.write(data)
  153. return response
  154. @login_required(login_url=reverse_lazy('login'))
  155. def getips(request):
  156. """
  157. Return the IPs and interfaces
  158. """
  159. try:
  160. get_ips = services.get_ipaddress()
  161. except Exception:
  162. get_ips = None
  163. data = json.dumps(get_ips['itfip'])
  164. response = HttpResponse()
  165. response['Content-Type'] = "text/javascript"
  166. response.write(data)
  167. return response
  168. @login_required(login_url=reverse_lazy('login'))
  169. def getusers(request):
  170. """
  171. Return online users
  172. """
  173. try:
  174. online_users = services.get_users()
  175. except Exception:
  176. online_users = None
  177. data = json.dumps(online_users)
  178. response = HttpResponse()
  179. response['Content-Type'] = "text/javascript"
  180. response.write(data)
  181. return response
  182. @login_required(login_url=reverse_lazy('login'))
  183. def getproc(request):
  184. """
  185. Return the running processes
  186. """
  187. try:
  188. processes = services.get_cpu_usage()
  189. processes = processes['all']
  190. except Exception:
  191. processes = None
  192. data = json.dumps(processes)
  193. response = HttpResponse()
  194. response['Content-Type'] = "text/javascript"
  195. response.write(data)
  196. return response
  197. @login_required(login_url=reverse_lazy('login'))
  198. def cpuusage(request):
  199. """
  200. Return CPU Usage in %
  201. """
  202. try:
  203. cpu_usage = services.get_cpu_usage()
  204. except Exception:
  205. cpu_usage = 0
  206. cpu = [
  207. {
  208. "value": cpu_usage['free'],
  209. "color": "#0AD11B"
  210. },
  211. {
  212. "value": cpu_usage['used'],
  213. "color": "#F7464A"
  214. }
  215. ]
  216. data = json.dumps(cpu)
  217. response = HttpResponse()
  218. response['Content-Type'] = "text/javascript"
  219. response.write(data)
  220. return response
  221. @login_required(login_url=reverse_lazy('login'))
  222. def memusage(request):
  223. """
  224. Return Memory Usage in % and numeric
  225. """
  226. datasets_free = []
  227. datasets_used = []
  228. try:
  229. mem_usage = services.get_mem()
  230. except Exception:
  231. mem_usage = 0
  232. try:
  233. cookies = request._cookies['memory_usage']
  234. except Exception:
  235. cookies = None
  236. if not cookies:
  237. datasets_free.append(0)
  238. datasets_used.append(0)
  239. else:
  240. datasets = json.loads(cookies)
  241. datasets_free = datasets[0]
  242. datasets_used = datasets[1]
  243. if len(datasets_free) > 10:
  244. while datasets_free:
  245. del datasets_free[0]
  246. if len(datasets_free) == 10:
  247. break
  248. if len(datasets_used) > 10:
  249. while datasets_used:
  250. del datasets_used[0]
  251. if len(datasets_used) == 10:
  252. break
  253. if len(datasets_free) <= 9:
  254. datasets_free.append(int(mem_usage['free']))
  255. if len(datasets_free) == 10:
  256. datasets_free.append(int(mem_usage['free']))
  257. del datasets_free[0]
  258. if len(datasets_used) <= 9:
  259. datasets_used.append(int(mem_usage['usage']))
  260. if len(datasets_used) == 10:
  261. datasets_used.append(int(mem_usage['usage']))
  262. del datasets_used[0]
  263. # Some fix division by 0 Chart.js
  264. if len(datasets_free) == 10:
  265. if sum(datasets_free) == 0:
  266. datasets_free[9] += 0.1
  267. if sum(datasets_free) / 10 == datasets_free[0]:
  268. datasets_free[9] += 0.1
  269. memory = {
  270. 'labels': [""] * 10,
  271. 'datasets': [
  272. {
  273. "fillColor": "rgba(249,134,33,0.5)",
  274. "strokeColor": "rgba(249,134,33,1)",
  275. "pointColor": "rgba(249,134,33,1)",
  276. "pointStrokeColor": "#fff",
  277. "data": datasets_used
  278. },
  279. {
  280. "fillColor": "rgba(43,214,66,0.5)",
  281. "strokeColor": "rgba(43,214,66,1)",
  282. "pointColor": "rgba(43,214,66,1)",
  283. "pointStrokeColor": "#fff",
  284. "data": datasets_free
  285. }
  286. ]
  287. }
  288. cookie_memory = [datasets_free, datasets_used]
  289. data = json.dumps(memory)
  290. response = HttpResponse()
  291. response['Content-Type'] = "text/javascript"
  292. response.cookies['memory_usage'] = cookie_memory
  293. response.write(data)
  294. return response
  295. @login_required(login_url=reverse_lazy('login'))
  296. def loadaverage(request):
  297. """
  298. Return Load Average numeric
  299. """
  300. datasets = []
  301. try:
  302. load_average = services.get_load()
  303. except Exception:
  304. load_average = 0
  305. try:
  306. cookies = request._cookies['load_average']
  307. except Exception:
  308. cookies = None
  309. if not cookies:
  310. datasets.append(0)
  311. else:
  312. datasets = json.loads(cookies)
  313. if len(datasets) > 10:
  314. while datasets:
  315. del datasets[0]
  316. if len(datasets) == 10:
  317. break
  318. if len(datasets) <= 9:
  319. datasets.append(float(load_average))
  320. if len(datasets) == 10:
  321. datasets.append(float(load_average))
  322. del datasets[0]
  323. # Some fix division by 0 Chart.js
  324. if len(datasets) == 10:
  325. if sum(datasets) == 0:
  326. datasets[9] += 0.1
  327. if sum(datasets) / 10 == datasets[0]:
  328. datasets[9] += 0.1
  329. load = {
  330. 'labels': [""] * 10,
  331. 'datasets': [
  332. {
  333. "fillColor": "rgba(151,187,205,0.5)",
  334. "strokeColor": "rgba(151,187,205,1)",
  335. "pointColor": "rgba(151,187,205,1)",
  336. "pointStrokeColor": "#fff",
  337. "data": datasets
  338. }
  339. ]
  340. }
  341. data = json.dumps(load)
  342. response = HttpResponse()
  343. response['Content-Type'] = "text/javascript"
  344. response.cookies['load_average'] = datasets
  345. response.write(data)
  346. return response
  347. @login_required(login_url=reverse_lazy('login'))
  348. def gettraffic(request):
  349. """
  350. Return the traffic for the interface
  351. """
  352. datasets_in = []
  353. datasets_in_i = []
  354. datasets_out = []
  355. datasets_out_o = []
  356. label = "KBps"
  357. try:
  358. intf = services.get_ipaddress()
  359. intf = intf['interface'][0]
  360. traffic = services.get_traffic(intf)
  361. except Exception:
  362. traffic = 0
  363. try:
  364. cookies = request._cookies['traffic']
  365. except Exception:
  366. cookies = None
  367. if not cookies:
  368. datasets_in.append(0)
  369. datasets_in_i.append(0)
  370. datasets_out.append(0)
  371. datasets_out_o.append(0)
  372. else:
  373. datasets = json.loads(cookies)
  374. datasets_in = datasets[0]
  375. datasets_out = datasets[1]
  376. datasets_in_i = datasets[2]
  377. datasets_out_o = datasets[3]
  378. if len(datasets_in) > 10:
  379. while datasets_in:
  380. del datasets_in[0]
  381. if len(datasets_in) == 10:
  382. break
  383. if len(datasets_in_i) > 2:
  384. while datasets_in_i:
  385. del datasets_in_i[0]
  386. if len(datasets_in_i) == 2:
  387. break
  388. if len(datasets_out) > 10:
  389. while datasets_out:
  390. del datasets_out[0]
  391. if len(datasets_out) == 10:
  392. break
  393. if len(datasets_out_o) > 2:
  394. while datasets_out_o:
  395. del datasets_out_o[0]
  396. if len(datasets_out_o) == 2:
  397. break
  398. if len(datasets_in_i) <= 1:
  399. datasets_in_i.append(float(traffic['traffic_in']))
  400. if len(datasets_in_i) == 2:
  401. datasets_in_i.append(float(traffic['traffic_in']))
  402. del datasets_in_i[0]
  403. if len(datasets_out_o) <= 1:
  404. datasets_out_o.append(float(traffic['traffic_out']))
  405. if len(datasets_out_o) == 2:
  406. datasets_out_o.append(float(traffic['traffic_out']))
  407. del datasets_out_o[0]
  408. dataset_in = (
  409. float(((datasets_in_i[1] - datasets_in_i[0]) / 1024) / (TIME_JS_REFRESH_NET / 1000)))
  410. dataset_out = (
  411. float(((datasets_out_o[1] - datasets_out_o[0]) / 1024) / (TIME_JS_REFRESH_NET / 1000)))
  412. if dataset_in > 1024 or dataset_out > 1024:
  413. dataset_in = (float(dataset_in / 1024))
  414. dataset_out = (float(dataset_out / 1024))
  415. label = "MBps"
  416. if len(datasets_in) <= 9:
  417. datasets_in.append(dataset_in)
  418. if len(datasets_in) == 10:
  419. datasets_in.append(dataset_in)
  420. del datasets_in[0]
  421. if len(datasets_out) <= 9:
  422. datasets_out.append(dataset_out)
  423. if len(datasets_out) == 10:
  424. datasets_out.append(dataset_out)
  425. del datasets_out[0]
  426. # Some fix division by 0 Chart.js
  427. if len(datasets_in) == 10:
  428. if sum(datasets_in) == 0:
  429. datasets_in[9] += 0.1
  430. if sum(datasets_in) / 10 == datasets_in[0]:
  431. datasets_in[9] += 0.1
  432. traff = {
  433. 'labels': [label] * 10,
  434. 'datasets': [
  435. {
  436. "fillColor": "rgba(105,210,231,0.5)",
  437. "strokeColor": "rgba(105,210,231,1)",
  438. "pointColor": "rgba(105,210,231,1)",
  439. "pointStrokeColor": "#fff",
  440. "data": datasets_in
  441. },
  442. {
  443. "fillColor": "rgba(227,48,81,0.5)",
  444. "strokeColor": "rgba(227,48,81,1)",
  445. "pointColor": "rgba(227,48,81,1)",
  446. "pointStrokeColor": "#fff",
  447. "data": datasets_out
  448. }
  449. ]
  450. }
  451. cookie_traffic = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  452. data = json.dumps(traff)
  453. response = HttpResponse()
  454. response['Content-Type'] = "text/javascript"
  455. response.cookies['traffic'] = cookie_traffic
  456. response.write(data)
  457. return response
  458. @login_required(login_url=reverse_lazy('login'))
  459. def getdiskio(request):
  460. """
  461. Return the reads and writes for the drive
  462. """
  463. datasets_in = []
  464. datasets_in_i = []
  465. datasets_out = []
  466. datasets_out_o = []
  467. try:
  468. diskrw = services.get_disk_rw()
  469. diskrw = diskrw[0]
  470. except Exception:
  471. diskrw = 0
  472. try:
  473. cookies = request._cookies['diskrw']
  474. except Exception:
  475. cookies = None
  476. if not cookies:
  477. datasets_in.append(0)
  478. datasets_in_i.append(0)
  479. datasets_out.append(0)
  480. datasets_out_o.append(0)
  481. else:
  482. datasets = json.loads(cookies)
  483. datasets_in = datasets[0]
  484. datasets_out = datasets[1]
  485. datasets_in_i = datasets[2]
  486. datasets_out_o = datasets[3]
  487. if len(datasets_in) > 10:
  488. while datasets_in:
  489. del datasets_in[0]
  490. if len(datasets_in) == 10:
  491. break
  492. if len(datasets_in_i) > 2:
  493. while datasets_in_i:
  494. del datasets_in_i[0]
  495. if len(datasets_in_i) == 2:
  496. break
  497. if len(datasets_out) > 10:
  498. while datasets_out:
  499. del datasets_out[0]
  500. if len(datasets_out) == 10:
  501. break
  502. if len(datasets_out_o) > 2:
  503. while datasets_out_o:
  504. del datasets_out_o[0]
  505. if len(datasets_out_o) == 2:
  506. break
  507. if len(datasets_in_i) <= 1:
  508. datasets_in_i.append(int(diskrw[1]))
  509. if len(datasets_in_i) == 2:
  510. datasets_in_i.append(int(diskrw[1]))
  511. del datasets_in_i[0]
  512. if len(datasets_out_o) <= 1:
  513. datasets_out_o.append(int(diskrw[2]))
  514. if len(datasets_out_o) == 2:
  515. datasets_out_o.append(int(diskrw[2]))
  516. del datasets_out_o[0]
  517. dataset_in = (
  518. int((datasets_in_i[1] - datasets_in_i[0]) / (TIME_JS_REFRESH_NET / 1000)))
  519. dataset_out = (
  520. int((datasets_out_o[1] - datasets_out_o[0]) / (TIME_JS_REFRESH_NET / 1000)))
  521. if len(datasets_in) <= 9:
  522. datasets_in.append(dataset_in)
  523. if len(datasets_in) == 10:
  524. datasets_in.append(dataset_in)
  525. del datasets_in[0]
  526. if len(datasets_out) <= 9:
  527. datasets_out.append(dataset_out)
  528. if len(datasets_out) == 10:
  529. datasets_out.append(dataset_out)
  530. del datasets_out[0]
  531. # Some fix division by 0 Chart.js
  532. if len(datasets_in) == 10:
  533. if sum(datasets_in) == 0:
  534. datasets_in[9] += 0.1
  535. if sum(datasets_in) / 10 == datasets_in[0]:
  536. datasets_in[9] += 0.1
  537. disk_rw = {
  538. 'labels': [""] * 10,
  539. 'datasets': [
  540. {
  541. "fillColor": "rgba(245,134,15,0.5)",
  542. "strokeColor": "rgba(245,134,15,1)",
  543. "pointColor": "rgba(245,134,15,1)",
  544. "pointStrokeColor": "#fff",
  545. "data": datasets_in
  546. },
  547. {
  548. "fillColor": "rgba(15,103,245,0.5)",
  549. "strokeColor": "rgba(15,103,245,1)",
  550. "pointColor": "rgba(15,103,245,1)",
  551. "pointStrokeColor": "#fff",
  552. "data": datasets_out
  553. }
  554. ]
  555. }
  556. cookie_diskrw = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  557. data = json.dumps(disk_rw)
  558. response = HttpResponse()
  559. response['Content-Type'] = "text/javascript"
  560. response.cookies['diskrw'] = cookie_diskrw
  561. response.write(data)
  562. return response