Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,4 +41,4 @@ proc disk_partitions*(all=false): seq[DiskPartition] =

# The type

- [DiskUsage](../types/DiskUsage.md)
- [DiskPartition](../types/DiskPartition.md)
19 changes: 19 additions & 0 deletions doc/linux/types/DiskPartition.md
Original file line number Diff line number Diff line change
@@ -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
12 changes: 2 additions & 10 deletions doc/windows/functions/disk_partitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions doc/windows/types/DiskPartition.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions src/psutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/psutil/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions src/psutil/psutil_linux.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" )
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions src/psutil/psutil_posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
51 changes: 41 additions & 10 deletions src/psutil/psutil_windows.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ proc pids*(): seq[int] =
for i in 0..<numberOfReturnedPIDs:
result.add( procArray[i].int )


proc pid_name*(processID: int): string =

#[
Expand All @@ -148,7 +149,7 @@ proc pid_name*(processID: int): string =

if EnumProcessModules( hProcess, hMod.addr, cast[DWORD](sizeof(hMod)), cbNeeded.addr):

GetModuleBaseName( hProcess, hMod, szProcessName,
GetModuleBaseName( hProcess, hMod, cast[LPWSTR](szProcessName.addr),
cast[DWORD](szProcessName.len) )

else:
Expand Down Expand Up @@ -190,9 +191,12 @@ proc pid_path*(pid: int): string =
cast[DWORD](pid))
defer: CloseHandle(processHandle)

if processHandle == cast[HANDLE](-1):
raiseError()

if processHandle.addr != nil or processHandle == cast[HANDLE](1) or processHandle == cast[HANDLE](NULL):

if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), filename, cast[PDWORD](dwSize.addr)) == FALSE:
if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), cast[LPSTR](filename.addr), cast[PDWORD](dwSize.addr)) == FALSE:

raiseError()

Expand Down Expand Up @@ -229,9 +233,12 @@ proc try_pid_path*(pid: int): string =
cast[DWORD](pid))
defer: CloseHandle(processHandle)

if processHandle == cast[HANDLE](-1):
raiseError()

if processHandle.addr != nil or processHandle == cast[HANDLE](1) or processHandle == cast[HANDLE](NULL):

if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), filename, cast[PDWORD](dwSize.addr)) == FALSE:
if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), cast[LPSTR](filename.addr), cast[PDWORD](dwSize.addr)) == FALSE:

result = ""

Expand Down Expand Up @@ -308,6 +315,7 @@ proc pid_arch*(pid: int) : int =

hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid)
defer: CloseHandle(hProcess)

if hProcess == cast[HANDLE](-1):
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwPid)
if hProcess == cast[HANDLE](-1):
Expand Down Expand Up @@ -355,7 +363,7 @@ proc pid_user*(pid: int): string =
GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr))


if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE:
if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE:
raiseError()

let user = wcUser[0..^1]
Expand Down Expand Up @@ -409,7 +417,7 @@ proc try_pid_user*(pid: int): string =
GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr))


if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE:
if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE:
return ""

let user = wcUser[0..^1]
Expand Down Expand Up @@ -462,7 +470,7 @@ proc pid_domain*(pid: int): string =

GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr))

if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE:
if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE:
raiseError()

let domain = wcDomain[0..^1]
Expand Down Expand Up @@ -512,7 +520,7 @@ proc pid_domain_user*(pid: int): (string, string) =
GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr)) #== FALSE:
# raiseError()

if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE:
if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE:
raiseError()

let user = wcUser[0..^1]
Expand Down Expand Up @@ -979,15 +987,38 @@ proc process_exists*(processName: string): bool =
return exists

proc pid_exists*(pid: int): bool =
## Function to check if specified pid exists
## will attempt to open the specified pid, and
## check if the return of WaitForSingleObject is equal to WAIT_TIMEOUT
var handle = OpenProcess(SYNCHRONIZE, FALSE, cast[DWORD](pid));
defer: CloseHandle(handle)

if handle == cast[HANDLE](-1):
raiseError()

var p = OpenProcess(SYNCHRONIZE, FALSE, cast[DWORD](pid));
var r = WaitForSingleObject(p, 0);
CloseHandle(p);
var r = WaitForSingleObject(handle, 0);
return r == WAIT_TIMEOUT



proc pid_cmdline*(pid: int): string =
raise newException( Exception, "Function is unimplemented!")

proc pid_kill*(pid: int) =

## Function for killing the specified pid
## will check if pid exists, and if not raises OSError
## if the pid does exist, then will attempt to OpenProcess
if not pid_exists(pid):
raise newException(OSError, "PID " & $(pid) & " doesn't exist")

var handle = OpenProcess(0x001F0FFF, FALSE, cast[DWORD](pid))
defer: CloseHandle(handle)

if handle == cast[HANDLE](-1):
raiseError()

if TerminateProcess(handle, 0):
return
else:
raiseError()