# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Gather process information."""

import os
import sys

if __name__ == "__main__":
    try:
        import sigma
    except ImportError:
        sys.path.append(os.path.join(os.pardir, os.pardir))

def Gather(collector):
    if os.name == "nt":
        from sigma.gather import gather_winps
        gather_winps.Gather_unixps(collector)
        return
    from sigma.gather.ps import ps
    it = ps()
    for record in it:
        # Check and skip header line
        assert record[:-1] == "UID PID PPID C STIME TTY TIME".split()
        assert record[-1] in ("CMD", "COMMAND")
        break
    # XXX The 'C' column is useless AFAICT
    # XXX The 'STIME' column should be converted to a real date
    #     (right now it is a string whose interpretation depends
    #     on the current date/time)
    # We can't call the uid column "uid" because that's an Oracle
    # reserved word and therefore can't be used as an Oracle database
    # column name.  Call it userid instead.
    colnames = "userid pid ppid c stime tty time cmd".split()
    table = collector.table("ps", colnames)
    for record in it:
        # On Solaris, defunct processes give a row that has too few
        # fields, so skip the row if it has too few fields.
        if len(record) == 8:
            # Fix bug 364, can't gather when the command column is too long.
            # On Red Hat 9, ps -efcww apparently skips commands that
            # are too long anyway, so this won't ever matter.
            record[-1] = record[-1][0:4000]
            table.add(record)

def _test():
    from sigma.gather import gathering
    gathering.test(Gather)

if __name__ == "__main__":
    _test()
