pstats: Include client pid with hello message

Useful for uniquely identifying the process in a situation where multiple clients on the same host connect with the same server.

New version bump is not necessary, as old servers should just ignore the extra field in the datagram.
This commit is contained in:
rdb 2022-01-30 00:23:55 +01:00
parent 8034cb5a92
commit 36edb22fd1
7 changed files with 32 additions and 4 deletions

View File

@ -39,6 +39,7 @@ encode(Datagram &datagram) const {
datagram.add_string(_client_progname); datagram.add_string(_client_progname);
datagram.add_uint16(_major_version); datagram.add_uint16(_major_version);
datagram.add_uint16(_minor_version); datagram.add_uint16(_minor_version);
datagram.add_uint32(_client_pid);
break; break;
case T_define_collectors: case T_define_collectors:
@ -86,6 +87,11 @@ decode(const Datagram &datagram, PStatClientVersion *version) {
_major_version = source.get_uint16(); _major_version = source.get_uint16();
_minor_version = source.get_uint16(); _minor_version = source.get_uint16();
} }
if (source.get_remaining_size() >= 4) {
_client_pid = source.get_uint32();
} else {
_client_pid = -1;
}
break; break;
case T_define_collectors: case T_define_collectors:

View File

@ -47,6 +47,7 @@ public:
// Used for T_hello // Used for T_hello
std::string _client_hostname; std::string _client_hostname;
std::string _client_progname; std::string _client_progname;
int _client_pid;
int _major_version; int _major_version;
int _minor_version; int _minor_version;

View File

@ -396,6 +396,11 @@ send_hello() {
message._type = PStatClientControlMessage::T_hello; message._type = PStatClientControlMessage::T_hello;
message._client_hostname = get_hostname(); message._client_hostname = get_hostname();
message._client_progname = _client_name; message._client_progname = _client_name;
#ifdef _WIN32
message._client_pid = GetCurrentProcessId();
#else
message._client_pid = getpid();
#endif
message._major_version = get_current_pstat_major_version(); message._major_version = get_current_pstat_major_version();
message._minor_version = get_current_pstat_minor_version(); message._minor_version = get_current_pstat_minor_version();

View File

@ -69,3 +69,11 @@ INLINE std::string PStatMonitor::
get_client_progname() const { get_client_progname() const {
return _client_progname; return _client_progname;
} }
/**
* Returns the process id of the client, or -1 if it is not known.
*/
INLINE int PStatMonitor::
get_client_pid() const {
return _client_pid;
}

View File

@ -38,10 +38,11 @@ PStatMonitor::
* indicates the client's reported hostname and program name. * indicates the client's reported hostname and program name.
*/ */
void PStatMonitor:: void PStatMonitor::
hello_from(const string &hostname, const string &progname) { hello_from(const string &hostname, const string &progname, int pid) {
_client_known = true; _client_known = true;
_client_hostname = hostname; _client_hostname = hostname;
_client_progname = progname; _client_progname = progname;
_client_pid = pid;
got_hello(); got_hello();
} }
@ -52,12 +53,13 @@ hello_from(const string &hostname, const string &progname) {
* effect. * effect.
*/ */
void PStatMonitor:: void PStatMonitor::
bad_version(const string &hostname, const string &progname, bad_version(const string &hostname, const string &progname, int pid,
int client_major, int client_minor, int client_major, int client_minor,
int server_major, int server_minor) { int server_major, int server_minor) {
_client_known = true; _client_known = true;
_client_hostname = hostname; _client_hostname = hostname;
_client_progname = progname; _client_progname = progname;
_client_pid = 0;
got_bad_version(client_major, client_minor, got_bad_version(client_major, client_minor,
server_major, server_minor); server_major, server_minor);
} }

View File

@ -43,8 +43,10 @@ public:
PStatMonitor(PStatServer *server); PStatMonitor(PStatServer *server);
virtual ~PStatMonitor(); virtual ~PStatMonitor();
void hello_from(const std::string &hostname, const std::string &progname); void hello_from(const std::string &hostname, const std::string &progname,
int pid);
void bad_version(const std::string &hostname, const std::string &progname, void bad_version(const std::string &hostname, const std::string &progname,
int pid,
int client_major, int client_minor, int client_major, int client_minor,
int server_major, int server_minor); int server_major, int server_minor);
void set_client_data(PStatClientData *client_data); void set_client_data(PStatClientData *client_data);
@ -63,6 +65,7 @@ public:
INLINE bool is_client_known() const; INLINE bool is_client_known() const;
INLINE std::string get_client_hostname() const; INLINE std::string get_client_hostname() const;
INLINE std::string get_client_progname() const; INLINE std::string get_client_progname() const;
INLINE int get_client_pid() const;
PStatView &get_view(int thread_index); PStatView &get_view(int thread_index);
PStatView &get_level_view(int collector_index, int thread_index); PStatView &get_level_view(int collector_index, int thread_index);
@ -98,6 +101,7 @@ private:
bool _client_known; bool _client_known;
std::string _client_hostname; std::string _client_hostname;
std::string _client_progname; std::string _client_progname;
int _client_pid;
typedef pmap<int, PStatView> Views; typedef pmap<int, PStatView> Views;
Views _views; Views _views;

View File

@ -195,11 +195,13 @@ handle_client_control_message(const PStatClientControlMessage &message) {
(message._major_version == server_major_version && (message._major_version == server_major_version &&
message._minor_version > server_minor_version)) { message._minor_version > server_minor_version)) {
_monitor->bad_version(message._client_hostname, message._client_progname, _monitor->bad_version(message._client_hostname, message._client_progname,
message._client_pid,
message._major_version, message._minor_version, message._major_version, message._minor_version,
server_major_version, server_minor_version); server_major_version, server_minor_version);
_monitor->close(); _monitor->close();
} else { } else {
_monitor->hello_from(message._client_hostname, message._client_progname); _monitor->hello_from(message._client_hostname, message._client_progname,
message._client_pid);
} }
} }
break; break;