system.disk.space.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "net/http"
  4. "os/exec"
  5. "strings"
  6. "encoding/json"
  7. "log"
  8. "runtime"
  9. )
  10. /*
  11. Disk Space Services
  12. Return the disk information of the system.
  13. The most basic task of all
  14. */
  15. type LogicalDiskSpaceInfo struct{
  16. Device string
  17. Volume int64
  18. Used int64
  19. Available int64
  20. UsedPercentage string
  21. MountPoint string
  22. }
  23. func system_disk_space_init(){
  24. //Register API
  25. http.HandleFunc("/system/disk/space/list", system_disk_space_handleList)
  26. //Register settings
  27. registerSetting(settingModule{
  28. Name: "Disk Space",
  29. Desc: "System Storage Space on Disks",
  30. IconPath: "SystemAO/disk/space/img/small_icon.png",
  31. Group: "Disk",
  32. StartDir: "SystemAO/disk/space/diskspace.html",
  33. })
  34. }
  35. func system_disk_space_handleList(w http.ResponseWriter, r *http.Request){
  36. allDisksVolume := system_disk_space_getDriveInfo();
  37. jsonString, _ := json.Marshal(allDisksVolume);
  38. sendJSONResponse(w, string(jsonString))
  39. }
  40. func system_disk_space_getDriveInfo() []LogicalDiskSpaceInfo{
  41. if runtime.GOOS == "windows" {
  42. //Check window disk info, wip
  43. cmd := exec.Command("wmic", "logicaldisk","get","caption,size,freespace")
  44. out, err := cmd.CombinedOutput()
  45. if (err != nil){
  46. log.Println("wmic not supported.")
  47. return []LogicalDiskSpaceInfo{};
  48. }
  49. lines := strings.Split(string(out),"\n")
  50. var results []LogicalDiskSpaceInfo
  51. for _,line := range lines{
  52. if (strings.Contains(line, ":")){
  53. //This is a valid drive
  54. line = strings.TrimSpace(line)
  55. //Tidy the line
  56. for strings.Contains(line, " "){
  57. line = strings.Replace(line, " "," ",-1)
  58. }
  59. //Split by space
  60. infoChunk := strings.Split(line, " ")
  61. if len(infoChunk) == 1{
  62. //Drive reserved and not mounted, like SD card adapters
  63. results = append(results, LogicalDiskSpaceInfo{
  64. Device: infoChunk[0],
  65. Volume: 0,
  66. Used: 0,
  67. Available: 0,
  68. UsedPercentage: "Not Mounted",
  69. MountPoint: infoChunk[0],
  70. })
  71. }else if (len(infoChunk) > 2){
  72. size, err := StringToInt64(infoChunk[2])
  73. if (err != nil){
  74. size = 0;
  75. }
  76. freespace, err := StringToInt64(infoChunk[1])
  77. if (err != nil){
  78. size = 0;
  79. }
  80. usedSpace := size - freespace;
  81. percentage := int64(float64(usedSpace) / float64(size) * 100)
  82. results = append(results, LogicalDiskSpaceInfo{
  83. Device: infoChunk[0],
  84. Volume: size,
  85. Used: usedSpace,
  86. Available: freespace,
  87. UsedPercentage: IntToString(int(percentage)) + "%",
  88. MountPoint: infoChunk[0],
  89. })
  90. }
  91. }
  92. }
  93. return results
  94. }else{
  95. //Get drive status using df command
  96. cmdin := `df -k | sed -e /Filesystem/d`
  97. cmd := exec.Command("bash","-c",cmdin)
  98. dev, err := cmd.CombinedOutput()
  99. if err != nil {
  100. dev = []byte{}
  101. }
  102. drives := strings.Split(string(dev), "\n")
  103. if (len(drives) == 0){
  104. return []LogicalDiskSpaceInfo{}
  105. }
  106. var arr []LogicalDiskSpaceInfo
  107. for _, driveInfo := range drives {
  108. if (driveInfo == ""){
  109. continue;
  110. }
  111. for strings.Contains(driveInfo, " "){
  112. driveInfo = strings.Replace(driveInfo, " ", " ", -1)
  113. }
  114. driveInfoChunk := strings.Split(driveInfo, " ")
  115. volume, _ := StringToInt64(driveInfoChunk[1])
  116. usedSpace, _ := StringToInt64(driveInfoChunk[2])
  117. freespaceInByte, _ := StringToInt64(driveInfoChunk[3])
  118. LogicalDisk := LogicalDiskSpaceInfo{
  119. Device: driveInfoChunk[0],
  120. Volume: volume * 1024,
  121. Used: usedSpace * 1024,
  122. Available: freespaceInByte * 1024,
  123. UsedPercentage: driveInfoChunk[4],
  124. MountPoint: driveInfoChunk[5],
  125. }
  126. arr = append(arr, LogicalDisk)
  127. }
  128. return arr
  129. }
  130. return []LogicalDiskSpaceInfo{}
  131. }