2

I need to monitor servers in a cross-platform environment : Windows, Unix (Aix, Solaris) and Linux. It means : get processes, find some files, ...

So i am focusing on JNA (Java Native Access) in order to deal with this. With the hope of being able to have one code to "rule them all".

Please, note that i am not a C developer.

So i don't know "C standard Library" or POSIX library for instance.

My questions :

how could i discover list of running processes in a cross-platform way ? Is JNA likely to do this ? Which function(s) to invoke, from which library (if not C standard library) ?

Thanks a lot for your advises and feedback.

SylvainR
  • 133
  • 1
  • 13
  • see [http://stackoverflow.com/questions/54686](http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java) – jan.supol Jun 29 '16 at 15:26
  • I have already parsed this article, and the use of JNA is mentionned only in the case of Windows environment. Does it mean there is no mean - with JNA - to do what i want in a cross-platform way ? And worst : does it mean there is no way to do it at all this on UX platforms ? – SylvainR Jun 29 '16 at 15:32
  • Process control and monitoring is unique to any given platform, so you'll have to write platform-specific code to do so (at least with JNA). You may be able to find a library that _uses_ JNA to present a _somewhat_ common API for process control, but the specifics of process control vary widely by platform. – technomage Jun 29 '16 at 17:54

1 Answers1

4

The other post linked in the comments lists the command line ps (for *nix) and taskinfo (for Windows) as the easiest approach. It doesn't use JNA but it gets you the answer reasonably reliably with little fuss.

I am the lead developer of the OSHI project which does exactly what you ask for Windows and macOS using JNA, and Linux using plain Java (one JNA function to follow a dynamic link), and four Unix flavors (AIX, Solaris, FreeBSD and OpenBSD) using the procfs. Source code is on that site for you to peruse at your leisure, or you can simply use that project as a dependency; you can find the getProcesses() methods in the platform-specific implementations of the OperatingSystem interface.

The short answer is that while you can use JNA it's not mandatory. In OSHI I use JNA's Platform class to determine which system you're running on (although even that is simply parsing the results of System.getProperty("os.name")) and based on which platform you are on, you branch off into platform-specific code.

For Linux, you simply read the /proc directory; every numeric file in that directory represents a process and has stats in the directory tree. This doesn't use JNA, but fit's much faster than trying to parse the C source code of the ps command which essentially leads to the same underlying information.

For Unix, parsing ps is an option. The -o flag lets you specify the fields you want to include.

For Windows you can use the Windows Process and Thread Functions (most of which are implemented in JNA already) or using the PSAPI functions via JNA as shown here, but the information returned is limited and requires a lot of jumping through hoops to get at details, some of which require elevated permissions. It's possible to query WMI via a COM interface in JNA to get all the info in one query. OSHI uses the registry to get most information from the same location WMI eventually would call.

For macOS X you use the proc_listpids() function to get the process IDs and feed that to proc_pidinfo(). These are in the System framework so JNA is used to implement the C calls in Java.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • Thx a lot, Daniel. A great enlightment on this topic. If your OSHI projet supports already *nix (other than Linux) platforms, i would probably switch on immediately. Infortunately, i need to provide software running on Aix, Solaris and misc other *nix. Too bad. Truly – SylvainR Jun 30 '16 at 06:57
  • If the other *nix platforms use the /proc filesystem then you can just adapt the same code as the Linux module. AIX and Solaris both use procfs so the code I wrote for Linux will work for them... not plug and play but enough for you to build your own solution, so I think this answers your question. You could always join the project and contribute Aix and Solaris ports ... I believe most of the Linux code would actually work. :) – Daniel Widdis Jun 30 '16 at 07:38
  • @SylvainR FYI, I've ported OSHI to FreeBSD and Solaris, and updated my answer here. Don't have access to AIX but happy to try to work with someone who does have access to see what needs to be changed. – Daniel Widdis Jul 27 '16 at 08:52