diff --git a/doc/linux/functions/disk_partions.md b/doc/linux/functions/disk_partitions.md similarity index 91% rename from doc/linux/functions/disk_partions.md rename to doc/linux/functions/disk_partitions.md index 4697d5e..91cd309 100644 --- a/doc/linux/functions/disk_partions.md +++ b/doc/linux/functions/disk_partitions.md @@ -1,6 +1,6 @@ -# disk_partions +# disk_partitions -This function returns a type DiskUsage. +This function returns a sequence of type DiskPartition. # The function ```nim @@ -41,4 +41,4 @@ proc disk_partitions*(all=false): seq[DiskPartition] = # The type -- [DiskUsage](../types/DiskUsage.md) \ No newline at end of file +- [DiskPartition](../types/DiskPartition.md) \ No newline at end of file diff --git a/doc/linux/types/DiskPartition.md b/doc/linux/types/DiskPartition.md new file mode 100644 index 0000000..3a43d12 --- /dev/null +++ b/doc/linux/types/DiskPartition.md @@ -0,0 +1,19 @@ +# DiskPartition + +DiskPartition is a type that hold disk partition information + +# the type + +```nim +type DiskPartition* = object of RootObj + device*: string + mountpoint*: string + fstype*: string + opts*: string +``` + +# information +device : the device name of the partition +mountpoint : the mountpoint of the partition +fstype : the file system type of the partition +opts : options of the partition \ No newline at end of file diff --git a/doc/windows/functions/disk_partitions.md b/doc/windows/functions/disk_partitions.md index 3a33119..f7695f8 100644 --- a/doc/windows/functions/disk_partitions.md +++ b/doc/windows/functions/disk_partitions.md @@ -69,14 +69,6 @@ proc disk_partitions*( all=false ): seq[DiskPartition] = discard SetErrorMode( 0 ) ``` -# Type DiskPartition - -```nim -type DiskPartition* = object of RootObj - device*: string - mountpoint*: string - fstype*: string - opts*: string - -``` +# The type +- [DiskPartition](../types/DiskPartition.md) diff --git a/doc/windows/types/DiskPartition.md b/doc/windows/types/DiskPartition.md new file mode 100644 index 0000000..3a43d12 --- /dev/null +++ b/doc/windows/types/DiskPartition.md @@ -0,0 +1,19 @@ +# DiskPartition + +DiskPartition is a type that hold disk partition information + +# the type + +```nim +type DiskPartition* = object of RootObj + device*: string + mountpoint*: string + fstype*: string + opts*: string +``` + +# information +device : the device name of the partition +mountpoint : the mountpoint of the partition +fstype : the file system type of the partition +opts : options of the partition \ No newline at end of file diff --git a/src/psutil.nim b/src/psutil.nim index dc876bd..7a39633 100644 --- a/src/psutil.nim +++ b/src/psutil.nim @@ -53,6 +53,10 @@ proc pid_exists*( pid: int ): bool = else: return platform.pid_exists(pid) +proc pid_kill*(pid: int) = + ## Function for killing specified pid + ## will raise OSError on error + platform.pid_kill(pid) proc cpu_count*(logical=true): int = # Return the number of logical CPUs in the system. @@ -278,6 +282,7 @@ export boot_time export uptime export users export pids +export pid_kill export cpu_times export per_cpu_times export cpu_stats @@ -291,3 +296,6 @@ export net_if_stats export disk_io_counters export per_disk_io_counters export net_connections + +# export the platform for access to all the procedures +export platform \ No newline at end of file diff --git a/src/psutil/common.nim b/src/psutil/common.nim index 9751d24..ee1ce40 100644 --- a/src/psutil/common.nim +++ b/src/psutil/common.nim @@ -111,7 +111,7 @@ proc usage_percent*[T](used: T, total: T, places=0): float = ## Calculate percentage usage of 'used' against 'total'. try: result = (used.int / total.int) * 100 - except DivByZeroError: + except DivByZeroDefect: result = if used is float or total is float: 0.0 else: 0 if places != 0: return round(result, places) diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index 35b66bd..1ec7592 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -2,7 +2,8 @@ import algorithm, math, net, os, posix, sequtils, sets, strutils, tables, times import strformat import common, psutil_posix - +from posix_utils import sendSignal +from posix import Pid, SIGKILL ################################################################################ const PROCFS_PATH = "/proc" @@ -206,6 +207,20 @@ proc pids_cmdline*(pids: seq[int]): seq[string] = for pid in pids: ret.add(pid_cmdline(pid)) +proc pid_kill*(pid: int) = + ## Function for sending a kill signal to the specified pid + if not pid_exists(pid): + raise newException(OSError, "PID " & $(pid) & " doesn't exist") + + var temp: Pid = int32(pid) + + try: + sendSignal(temp, SIGKILL) + except OSError: + raise getCurrentException() + + + proc pid_name*(pid: int): string = ## Function for getting the process name of a pid ## not to be mixed with pid_cmdline. This only gets the @@ -644,7 +659,7 @@ proc swap_memory*(): SwapMemory = percent:percent, sin:0, sout:0 ) # try to get pgin/pgouts - if not existsFile( PROCFS_PATH / "vmstat" ): + if not fileExists( PROCFS_PATH / "vmstat" ): # see https://github.com/giampaolo/psutil/issues/722 echo( "'sin' and 'sout' swap memory stats couldn't be determined ", "and were set to 0" ) @@ -706,7 +721,7 @@ proc per_nic_net_io_counters*(): TableRef[string, NetIO] = for line in lines( PROCFS_PATH / "net/dev" ): if not( ":" in line ): continue let colon = line.rfind(':') - let name = line[..colon].strip() + let name = line[0..colon].strip() let lst = line[(colon + 1)..len(line) - 1].strip.replace("\x00", "").splitWhitespace let fields = mapIt(lst, parseInt(it)) @@ -903,7 +918,7 @@ iterator process_inet( file: string, family: int, socketType: int, inodes : Orde for line in file.lines: try: - let strings = line.splitWhitespace()[..10] + let strings = line.splitWhitespace()[0..10] laddr = strings[1] raddr = strings[2] status = strings[3] diff --git a/src/psutil/psutil_posix.nim b/src/psutil/psutil_posix.nim index 6878e66..8c13e41 100644 --- a/src/psutil/psutil_posix.nim +++ b/src/psutil/psutil_posix.nim @@ -37,15 +37,15 @@ type mem_start*: culong mem_end*: culong base_addr*: cushort - irq*: cuchar - dma*: cuchar - port*: cuchar ## # 3 bytes spare + irq*: char + dma*: char + port*: char ## # 3 bytes spare type - INNER_C_UNION_9261176668105079294* = object {.union.} + INNER_C_UNION_9261176668105079294* {.union.} = object ifrn_name*: array[IFNAMSIZ, char] ## # Interface name, e.g. "en0". - INNER_C_UNION_7660000764852079517* = object {.union.} + INNER_C_UNION_7660000764852079517* {.union.} = object ifru_addr*: SockAddr ifru_dstaddr*: SockAddr ifru_broadaddr*: SockAddr @@ -156,7 +156,7 @@ proc psutil_convert_ipaddr(address: ptr SockAddr, family: posix.TSa_Family): str else: addrlen = sizeof(SockAddr_in6).uint32 - let err = getnameinfo( address, addrlen, result, resultLen, nil, 0, NI_NUMERICHOST ) + let err = getnameinfo( address, addrlen, cast[cstring](result), resultLen, nil, 0, NI_NUMERICHOST ) if err != 0: # // XXX we get here on FreeBSD when processing 'lo' / AF_INET6 # // broadcast. Not sure what to do other than returning None. diff --git a/src/psutil/psutil_windows.nim b/src/psutil/psutil_windows.nim index 8d254c4..d662afe 100644 --- a/src/psutil/psutil_windows.nim +++ b/src/psutil/psutil_windows.nim @@ -126,6 +126,7 @@ proc pids*(): seq[int] = for i in 0..